1
/*
 * File: Countdown.java
 * ----------------------
 * This program counts backwards from the value  START
 * to zero, as in the countdown preceding a rocket launch.
 */

import acm.program.*;

public class Countdown extends ConsoleProgram  {

    public void run () {
        for (int t = START; t >= 0; t-- ); {
            println(t);
        }
        println ("Liftoff!");
    }
/* Specifies the value from which to start the countdown.*/

    private static final int START = 10;
}

My problem is that the (t) in the following statement is not recognized as a variable:

println(t);

2 Answers 2

4

Change

for (int t = START; t >= 0; t-- ); {
                                 ^
    println(t);
}

to

for (int t = START; t >= 0; t--) {
    println(t);
}

The ; closed the for statement, which means the following {} block was not part of the loop, so t wasn't defined in the scope of that block.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much :) i'm obviously new :)
0

(This is arguably a comment on Eran's answer, but has to be posted as an answer to get the necessary formatting.)

You should use a Java-aware editor, and reformat regularly using it, but especially when you do not understand an error. I reformatted the code from the question using Eclipse. Here is the result:

for (int t = START; t >= 0; t--)
  ;
{
  println(t);
}

The significance of the ";", and the fact that the block is at the same level as the for-loop are both hidden by the original formatting and put front-and-center by the reformatting.

1 Comment

I use Eclipse myself. Never used a reformatting option. I am new, just trying to follow a book's exercises.

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.