如何循环?如何打印大于0的连续整数,每行三个?
我想这个循环:
0-> 1,2,3
1-> 4,5,6
2-> 7,8,9
3-> 10,11,12
4->.....
......
我不知道如何写这个算法。 我在下面尝试,它不起作用。
public class gYie {
public static void main(String[] args) {
int current = 0;
int death = 0;
for (int i = 0; i < 10; i++) {
System.out.print(i + " ");
for (int j = 0; j < 3; j++) {
System.out.print(death+j +" ");
current += j;
}
death += current;
System.out.println("");
}
}
}
其输出是:
run:
0 0 1 2
1 3 4 5
2 9 10 11
3 18 19 20
4 30 31 32
5 45 46 47
6 63 64 65
7 84 85 86
8 108 109 110
9 135 136 137
如何解决这个问题?我想不出如何写它。
3变为18,19,20而不是12,13,14。
输出应该是什么样子? – CloudyMarble 2013-04-26 05:04:55
检查我的答案,只使用一个循环的解决方案 – rajesh 2013-04-26 05:16:41