Java Nested loops: Indent text

We Are Going To Discuss About Java Nested loops: Indent text . So lets Start this Java Article.

Java Nested loops: Indent text

  1. Java Nested loops: Indent text

    This is a little late but I am doing this same challenge in zybooks for school. The above code marked solved is a little off. Using that code places a space at the beginning of the iteration so it should look more like this so the “whitespace” doesn't differ and because “int i” and “int j” were already defined in the main string…

  2. Java Nested loops: Indent text

    This is a little late but I am doing this same challenge in zybooks for school. The above code marked solved is a little off. Using that code places a space at the beginning of the iteration so it should look more like this so the “whitespace” doesn't differ and because “int i” and “int j” were already defined in the main string…

Solution 1

This is a little late but I am doing this same challenge in zybooks for school. The above code marked solved is a little off. Using that code places a space at the beginning of the iteration so it should look more like this so the “whitespace” doesn’t differ and because “int i” and “int j” were already defined in the main string…

for (i = 0; i < userNum; i++) {
   for (j = 1; j < i; j++) {
      System.out.print(" ");
   }
   System.out.println(i);
}

Remove the “int” from both “i” & “j” because they have already been defined in your main string. You also have to make “j = 1” otherwise it will put a space before 0 which will have a whitespace difference. Hope this helps anyone else who comes across this challenge.

Original Author RobertB922 Of This Content

Solution 2

I don’t think i and j are necessary in that sense…

for (int i = 0; i <= userNum; i++) {
    for (int j = 0; j < i; j++) {
        System.out.print(" ");
    }
    System.out.println(i);
}

Original Author Michael Of This Content

Solution 3

for (i = 0; i <= userNum; i++) {
    for (j = 0; j < i; j++) {
            System.out.print(" ");  
         }
         System.out.println(i);
      }

Original Author Sen GY Of This Content

Solution 4

So I actually ran the above codes in zybooks, and they didn’t work because the relational operators didn’t include the userNum, which threw off the result.

Try this:

  for (i = 0; i <= userNum; ++i) {
   for (j = 1; j <= i; ++j) {
     System.out.print(" ");
   }
   System.out.println(i);
}

Original Author SonOfGoose97 Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment