【发布时间】:2012-12-01 15:51:46
【问题描述】:
您好,我是编程新手并已注册此论坛:)
所以我创建了一个带有嵌套 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();
}
}
【问题讨论】:
-
我认为您正在寻找递归
-
是的,今天搜索了一下,有时会迷路,但我并没有真正明白如何实现。
-
为什么在循环中增加计数时,在循环中增加计数?
-
糟糕!这是一个错误..->谢谢!
标签: java algorithm loops nested