gpt4 book ai didi

Java 可自定义嵌套 for 循环的数量

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:29 26 4
gpt4 key购买 nike

我正在尝试制作可自定义数量的嵌套 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;
}
}

关于Java 可自定义嵌套 for 循环的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com