here is java code that prints triangles one below each other,
naming them A, B, C, D; my question is how to print them on*the same level*
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() ;
}
}
}
so the java code above will print this:
*
**
***
***
**
*
***
**
*
*
**
***
now i need to print same figures on the same level!
* *** *** *
** ** ** **
*** * * ***
please help me!
i m looking for your answer!
thanks in advance
解决方案
Why don't you want to print like this?
System.out.println("* *** *** *") ;
System.out.println("** ** ** **") ;
System.out.println("*** * * ***") ;
UPDATE: OK.
If this is homework for loops. Just create matrix (array of arrays) and print by assigned elements of the matrix.
Then print out the matrix line by line.
You should use offsets for each printing cycles. Fox example, for second in pseudo code (not java code!):
//*********************************
// (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;