I do not seem able to find the answer -
I need to attract the loop Use a simple triangle.
* *** ***** ******** ******************************************************************* ********* I can make a half triangle, but I do not know how to make a complete triangle to add to my current loop
* * * *** **** For ***** (int i = 0; i <6; i ++) {for (int j = 0; j & lt; i; j ++) { System.out.print ("*"); } System.out.println (""); } thanks -
First of all, make sure that You are producing the correct number of symbols * we need to produce 1, 2, 3 instead of 1, 3, 5 et setter. This counter variable can be fixed by modifying: I + = 2) {for (int j = 0; j & lt; i; j ++) {System.out.print ("*"); } System.out.println (""); } As you can see, to start this i with 1 and 2 Increases with each step as long as this 10 (i.e., 1 , 3 , 5 , 7 , 9 ). It gives us the correct number of * symbols, then we need to fix the indentation level per line. It can be done as follows: for (int i = 1; i <10; i + = 2) {for (int k = 0; k & lt; 4 - i / 2)); K ++) {System.out.print (""); } For (int j = 0; j & lt; i; j ++) {System.out.print ("*"); } System.out.println (""); } We print some spaces and before we print the number of spaces * , we depend on the line that is on us That is what is for loop with the of variable. We can see that k narrow the values 4 , 3 < / Code>, 2 , 1 and 0 when ¬ is 1 , < Code> 3 , 5 , 7 and 9 . This is what we want because in the triangle we are in, there are so many places that we need to give space. Next we come down the triangle, we need less space and the last row of the triangle does not need to have an empty space.
Comments
Post a Comment