我必须像打印三角形图案(半金字塔)
1
0 1
1 0 1
0 1 0 1
我尝试了这个程序
class tri{
public static void main(String arg[]){
int i,j,a = 1, b =0, c=0;
for(i=1; i<=4; i++){
for(j=1; j<=i; j++){
System.out.print(a+ " ");
c = a;
a = b;
b = c;
}
System.out.println();
}
}
}
但这会打印图像中所示的图案
如果有人可以帮助我编辑该代码以带来该模式,请
最短的形式是
String str = "";
for (int i = 1; i <= 4; i++) {
str = (i % 2) + " " + str;
System.out.println(str);
}
这将根据需要提供输出
1
0 1
1 0 1
0 1 0 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句