ERJ*_*JAN 0 java loops println
这里是java代码,它打印一个彼此相同的三角形,命名为A,B,C,D; 我的问题是如何在同一水平上打印它们*
public class ex_5_10 {
public static void main(String args[]){
// (A)
System.out.println("(A)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if(column > row) continue ;
System.out.print("*");
}
System.out.println() ;
}
//*********************************
// (B)
System.out.println("(B)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if(column < row) continue ;
System.out.print("*");
}
System.out.println() ;
}
//********************************
//(C)
System.out.println("(C)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if( column < row ) System.out.print(" ") ;
else System.out.print("*");
}
System.out.println() ;
}
// (D)
System.out.println("(D)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 10 ; column >= 0 ; column--){
if( column > row ){ System.out.print(" ") ; }
else {System.out.print("*"); }
}
System.out.println() ;
}
}
Run Code Online (Sandbox Code Playgroud)
}
所以上面的java代码会打印出来:
*
**
***
***
**
*
***
**
*
*
**
***
Run Code Online (Sandbox Code Playgroud)
现在我需要在同一水平上打印相同的数字!
* *** *** *
** ** ** **
*** * * ***
Run Code Online (Sandbox Code Playgroud)
请帮我!我正在寻找你的答案!提前致谢
你为什么不想这样打印?
System.out.println("* *** *** *") ;
System.out.println("** ** ** **") ;
System.out.println("*** * * ***") ;
Run Code Online (Sandbox Code Playgroud)
更新:好的.
如果这是循环的功课.只需创建矩阵(数组数组)并按矩阵的指定元素进行打印.
然后逐行打印出矩阵.
您应该为每个打印周期使用偏移.Fox的例子,伪代码中的第二个(不是java代码!):
//*********************************
// (B)
matrix[0][offset] = "B";
for(int row = 0 ; row < height ; row++){
for(int column = 0 ; column < width ; column++){
if(column < row) continue ;
matrix[row][column+offset] = "*";
}
}
offset += width + space_length;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16857 次 |
最近记录: |