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.
7 Respuestas
+ 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
}
}
+ 3
Martynas Demšė Check my code too
+ 2
Is this correct?
https://code.sololearn.com/cKui9UVSvrUi/?ref=app
+ 1
Here you have an example i have done that Will print what you need:
https://code.sololearn.com/c82jU4OG4Hqs/?ref=app
+ 1
Thanks a lot! 👍
+ 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?
+ 1
Thanks again