1

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...

1
  • 4
    for (int a = i; a <= i-10; a -=5);{ this will never execute. Commented Oct 23, 2013 at 22:10

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks -- I don't have the error any more, but I guess the code is wrong because the output is completely blank. I'll try again...
@user2913362 I have amended my answer with I believe is wrong with the condition on the a for loop.
0

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("");
    }
}

Comments

0

You need a >= i - 10 in the middle of the second loop, not <=. Also, remove that extra semicolon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.