0

Can you get this code to display "1-22-333-"?

I Can not order this code so to display "1-22-333-". Code: System.out.print("-"); for (int i=1; i<=3; i++) { System.out.print("-"); for (int i=1; i<=3; i++) { } } I get the answer always... "Oh, snap! We open the outer loop to count from 1 to 3. Then we open the inner loop to run for i times and print the numbers. After closing the inner loop, we print "-" and close the outer loop." Please help to put it in the right order.

24th Mar 2019, 8:13 PM
Martynas Demšė
Martynas Demšė - avatar
7 Answers
+ 6
for (int i=1; i<=3; i++) { //Runs from 1 to 3 if(i!=1) { System.out.print("-"); //Prints only when this is not the first run of the loop } for(int j=1; j<=i; j++) { //Runs from 1 to value of i in order to print that number of times System.out.print(i); Prints the number once } }
24th Mar 2019, 8:57 PM
Kartik
Kartik - avatar
+ 3
Martynas Demšė Check my code too
24th Mar 2019, 8:49 PM
Kartik
Kartik - avatar
24th Mar 2019, 8:48 PM
Kartik
Kartik - avatar
+ 1
Here you have an example i have done that Will print what you need: https://code.sololearn.com/c82jU4OG4Hqs/?ref=app
24th Mar 2019, 8:44 PM
Jorge
Jorge - avatar
+ 1
Thanks a lot! 👍
24th Mar 2019, 8:48 PM
Martynas Demšė
Martynas Demšė - avatar
+ 1
Could you be so kind my fellows and explain me how we get such answer as 1-22-333-? What really happens in the loop?
24th Mar 2019, 8:53 PM
Martynas Demšė
Martynas Demšė - avatar
+ 1
Thanks again
24th Mar 2019, 9:00 PM
Martynas Demšė
Martynas Demšė - avatar