我正在尝试使可定制的数量的嵌套for循环,像这样
int size=4;
for ...
for ...
for ...
for ...
当更改大小时,创建一个新的嵌套循环
int size=6;
for ...
for ...
for ...
for ...
for ...
for ...
我一直在尝试和测试,最后我得到了这个:
// The amount of nested loops
int size=3;
// Variables
String output="";
String code="";
String home=new File("").getAbsolutePath();
// Generating code
// Opening
code+="public class Temp { \n";
code+=" public static void main(String[] args) { \n";
String depth=" ";
// Making the variables
code+=depth+"int[] data = new int["+size+"];\n";
code+=depth+"String output = \"\";\n";
// Making the for loops
for(int i=0; i<size; i++) {
// Adding formatting
for(int x=i; x>0; x--)
code+=" ";
// Creating for-loop
code+=depth+"for (data["+i+"]=0; data["+i+"]<10; data["+i+"]++) {\n";
}
// Adding formatting
for(int x=0; x<size; x++)
code+=" ";
// Making the output (data[0]+""+data[1]+""+data[2]+"" etc)
code+=depth+"output+=";
for(int i=0; i<size; i++) {
code+="data["+i+"]+\"\"";
if(i<size-1)
code+="+";
}
code+=";\n";
// Adding formatting
for(int x=0; x<size; x++)
code+=" ";
// Adding a newline after the output
code+=depth+"output+=\"\\n\";\n";
// Adding formatting and closing for-loops
for(int i=0; i<size; i++) {
for(int x=i; x<size-1; x++)
code+=" ";
code+=depth+"}\n";
}
// Outputting the variable output
code+=depth+"System.out.println(output);\n";
code+=" }\n";
code+="}\n";
// Outputting the code (for debugging purposes)
System.out.println(code);
// Compiling, running and getting output
try {
// Making a file called Temp.java
FileOutputStream fos=new FileOutputStream("Temp.java");
byte[] buf=code.getBytes();
fos.write(buf);
fos.close();
System.out.println("===== ===== ===== ===== ===== =====");
System.out.println("\n===== Compiling =====\n");
// Executing
Process p=Runtime.getRuntime().exec("javac -d "+home+" Temp.java");
// Getting output and error
InputStream is=p.getInputStream();
InputStream es=p.getErrorStream();
buf=new byte[8192];
byte[] ebuf=new byte[8192];
is.read(buf);
es.read(ebuf);
System.out.println(new String(buf).trim());
System.err.println(new String(ebuf).trim());
System.out.println("\n===== Running =====");
// Executing
p=Runtime.getRuntime().exec("java Temp");
// Getting output and error
is=p.getInputStream();
es=p.getErrorStream();
buf=new byte[8192];
ebuf=new byte[8192];
is.read(buf);
es.read(ebuf);
// Make output the value of the external 'output'
output=new String(buf).trim();
System.err.println(new String(ebuf).trim());
System.out.println("\n===== Removing temp files =====");
// Executing
p=Runtime.getRuntime().exec("rm -f Temp.java Temp.class");
// Getting output and error
is=p.getInputStream();
es=p.getErrorStream();
buf=new byte[8192];
ebuf=new byte[8192];
is.read(buf);
es.read(ebuf);
System.out.println(new String(buf).trim());
System.err.println(new String(ebuf).trim());
System.out.println("\n===== ===== ===== ===== ===== =====");
} catch(Exception e) {
e.printStackTrace();
}
// Outputting the output
System.out.println("output\n"+output);
它可以正常工作,但是我讨厌创建,编译和运行外部Java文件的方式。有没有更好的方法可以在不使用外部文件的情况下做同样的事情?
最佳答案
递归就是答案(如上所述),或者在数组中管理“i”变量:
public void nestedLoop(int size, int loopSize) {
int[] i = new int[size];
while (i[size-1] < loopSize) {
doSomethingWith(i);
increment(i, loopSize);
}
}
public void increment(int[] i, int maxSize) {
int idx = 0;
while (idx < i.length) {
if (++i[idx] < maxSize) {
return;
}
i[idx++] = 0;
}
}