java while and for loops dilemma -
i m working on example , cannot seem figure doing wrong.
public class nestedloop { public static void main(string[] args) { int usernum = 0; int = 0; int j = 0; while (j <= usernum) { system.out.println(j); ++j; (i = j; <= usernum; ++i) { system.out.print(" "); } } return; } }
the result below. can see result backward , not sure doing wrong. have tried change variables around , not getting where. appreciate.
expected output: 0 1 2 3 output: 0 1 2 3
thanks
you using usernum
increment wrong , increment j++
after spaces return. can remove return statement.
corrected code:
public class nestedloop { public static void main(string[] args) { int usernum = 3; int = 0; int j = 0; while (j <= usernum) { system.out.println(j); (i = 0 ; <= j; ++i) { system.out.print(" "); } ++j; } } }
Comments
Post a Comment