class Num {
public static void main(String[] args) {
for(int i=1;i<=5;i++) {
for(int j=1;j<=i;j++) {
if(j==1) {
System.out.print(i);
}
else if(j==2) {
System.out.print(" "+(i+j+2));
}
else {
System.out.print(" "+(i+j+4));
}
}
System.out.println(" ");
}
}
}
输出:
1
2 6
3 7 10
4 8 11 12
5 9 12 13 14
预期:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
我尝试了很多并想出了这个逻辑
when j=1 then i
when j=2 then i+j+2
when j=3 then i+j+4
when j>=4 then i+j+5
这里总共有4个条件,我如何在嵌套的for循环中做到这一点。任何其他逻辑也是可以理解的。
最佳答案
int lineCount = 5;
for (int i = 1; i <= lineCount; i++) {
int value = i;
for (int j = 1; j <= i; j++) {
System.out.print(value + " ");
value += lineCount -j;
}
System.out.println("");
}
关于java - 打印下面提到的数字模式的逻辑是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26560299/