JAVA编写程序输出下列结果 1 1 2 1 2 3 1 2 3 4

是这种图形
1
1 2
1 2 3
1 2 3 4

for(int x=1;x<=4;x++)
{ switch(x)
{case 1:System.out.println("1");break;
case 2:System.out.println("1 2");break;
case 3:System.out.println("1 2 3");break;
case 4:System.out.println("1 2 3 4");break;
}

}
还可以用 break来改变循环
for(int x=1;x<=4;x++)
{
for(int y=1;y<=4;y++)
{ System.out.print(y+" ");
if(x==y)
break;
}
System.out.println();
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-31
for(int i = 1 ; i <= 4 ; i++){
for(int j = 1 ; j <= i ; j++){
System.out.print(j+" ");
}
System.out.println();
}
第2个回答  2011-03-31
一楼正解
相似回答