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
If you want to play with the results you can do the following: (It's a homework)
you can notice that in t he loops there are the following conditions:
if(column < row) continue ; System.out.print("*");
if(column < row) continue ; System.out.print("*");
if( column < row ) System.out.print(" ") ; else
System.out.print("*");if( column > row ){ System.out.print(" ") ; } else
{System.out.print("*"); }Just mix and re-arrange them, and see the out-coming results. And also move the insides of loops for the columns that are internal of one loop for the rows, and add offsets to the columns.
This time you need to use one block of loops and not 4.
Have fun.
There's no single-line answer to this.
You need to bear in mind that when you call
println()
, there will be a newline output at the end of your string, so nothing further will appear on that line.With that in mind, you'll need to split the logic, so that you assemble the outputs for the various "figures" first, and then once you've processed them, you output the result in a way that makes sense (so the first line of every character, then the second etc.). Instead of calling print while handling each figure, put the results into an appropriate data structure.
For bonus marks, you'll need to word-wrap when enough data is supplied to span the width of the display...
You would move the internal for loops for the columns inside of one loop for the rows, and add offsets to the columns. The logic for the internal if would then all be column - offset.
You could also try somethibng like this:
Maybe you can simplify it more by changing 4 inner loops to one loop. But it is your homework...