1

I need some help. I want my program to print different numbers according to the user's input and indent that number with that number of spaces as well. Must be using a nested loop.

Expected Output: (lets say user types 3)

0  
 1
  2
   3

for simplicity I'm only showing you what I did in the nested for loop and ignoring the user's input portion.

What I did:

for (i=0; i<=userNum; i++) 
  {
     for (j=0; j<=i; j++) 
     {
        ? (I think here is the problem as I don't know how to indent by that no of spaces)
     }
     System.out.println(i);
  }  

Thanks in advance!

Anonymous
  • 489
  • 1
  • 7
  • 18
  • What do you think should be inside that loop? What is its purpose? – Pshemo Nov 10 '16 at 17:14
  • @Pshemo, its purpose should be to indent with spaces according to the number for each line. I only know `System.out.println();`, but thats something else. – Anonymous Nov 10 '16 at 17:17
  • 1
    Hint: Beside `println(data)` there is also `print(data)` method. Difference is that `print(data)` doesn't automatically add line separator after `data`. This allows us to print `data` many times *in same line*. – Pshemo Nov 10 '16 at 17:19
  • 1
    @Pshemo, thanks a lot. I knew about this `print(data)`, but I never wonder it would be needed here. All credits to you :) Here is what I put inside nested loop `System.out.print(" "); ` and changed `j = 1` – Anonymous Nov 10 '16 at 17:24
  • 1
    Good job and good luck with your next exercises. One advice: get used to iterating from 0 since most things in Java (and in other languages) are indexed from `0` till `max-1` (like arrays). So instead of `for (int i = 1; i<=max; i++)` try to getting used to `for (int i = 0; i < max; i++)`. – Pshemo Nov 10 '16 at 17:40

1 Answers1

1

Inside the loop you should use

System.out.print(" ");
Loki
  • 801
  • 5
  • 13