Reputation: 29
I'm in a beginner class and my output should look like
25 20 15
26 21 16
27 22 17
28 23 18
This is my loop:
for (int i = 25; i <= 28; i++){
for (int a = i; a <= i-10; a -=5);{
System.out.print(a);
}
System.out.println("");
}
I can't figure out what's wrong with it, but it gives me an error message. Am I doing it right? Nested loops are really difficult for me...
Upvotes: 1
Views: 259
Reputation: 79875
You need a >= i - 10
in the middle of the second loop, not <=
. Also, remove that extra semicolon.
Upvotes: 0
Reputation: 23049
You have semicolon at the end of for cycle
for (int a = i; a <= i-10; a -=5);
Just remove it and here you go :
for (int a = i; a <= i-10; a -=5)
Also it is not fully functional, this code do output you want :
public static void main(String[] args) {
for (int i = 25; i <= 28; i++) {
for (int j = 0; j < 3; j++) {
System.out.print((i - j*5) +" ");
}
System.out.println("");
}
}
Upvotes: 0
Reputation: 178313
Remove the semicolon on this line:
for (int a = i; a <= i-10; a -=5);{
Java thinks that the semicolon is the body of the loop. Then a
in the next block is out of scope, giving an error.
Additionally, the condition looks wrong on that for
loop. If you start a
at i
, then it will start out NOT less than or equal to i - 10
. Perhaps you meant
a >= i - 10
Upvotes: 2