Friday, 15 February 2013

java - Convert this for loop into a while loop? -


This code is trying to convert a loop to a loop:

  Int endData = MyScanner.nextInt (); Int line = 0; Int cell = 0; Int rowcell = 0; System.out.println ("For Loop:"); (Line = 1; row & lt; = endData; row ++) {if (line% 2 == 0) {for (cell = 0; cell & lt; row; cell ++) {for (rowcell = 0; Rowcell & lt; = cell; Rowcell ++) {System.out.print (line); } System.out.println (""); }} Else {for (cell = row; cell>; cell--) {for (rowcell = cell; rowcell & gt; rowcell--) {System.out.print (line); } System.out.println (""); }}}  

So far I have tried this code but it was completely wrong:

  row2 = 1; While (line2 and lt; = endadata) {if (row2% 2 == 0) {cell2 = 0; Rowcell2 = 0; While (cell 2 and lieutenant; line 2) {cell 2++; While (line2 and lt; = cell 2) {rowcell2 ++; System.out.print (line2); } System.out.println (""); }} And {cell 2 = line 2; Rowcell2 = cell 2; While (cell2> 0) {cell 2 -; } While (rowseil 2> gt; {rowcell2--; System.out.print (line2); } Println (""); } Row2 ++; }  

For example, when the user inputs the value of 3, then gives the correct answer for the loop:






333
33
3

But while loop output: 1

22
2 333

How to know about fixing

Code> line2 = 0;

It should be

 under  (cell2lft; line2) { 

No comments:

Post a Comment