您好,我是编程新手并已注册此论坛 :)
所以我创建了一个带有嵌套 for 循环的小程序,它打印出五个数字的所有组合,其值可以从 0 到 5。使用嵌套的 for 循环可以正常工作。但是没有更清洁的解决方案吗?我尝试调用 for 循环本身,但我的大脑没有得到解决方案.. :(
//my ugly solution
int store1, store2, store3, store4, store5;
for (int count = 0; count <= 5; count++) {
store1 = count;
for (int count2 = 0; count2 <= 5; count2++) {
store2 = count2;
for (int count3 = 0; count3 <= 5; count3++) {
store3 = count3;
for (int count4 = 0; count4 <= 5; count4++) {
store4 = count4;
System.out
.println(store1 + " " + store2 + " " + store4);
}
//I'm trying around with something like this
void method1() {
for (int count = 0; count <= 5; count++) {
list.get(0).value = count;
count++;
method2();
}
}
void method2() {
for (int count = 0; count <= 5; count++) {
list.get(1).value = count;
count++;
method1();
}
}