简体   繁体   中英

Can't change a variable made outside a while loop inside the loop

public class Cow {
    public static void main(String [ ] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Figure out a number between 1 and 100");
        int num = 4;
        int guess = scan.nextInt();
        if (guess == num) {
            System.out.println("Congratulations, you got it on your first try!");
        }
        int tries = 1;
        while (guess != num) {
            System.out.println("Incorrect. You have guessed: " + tries 
                    + " times. Guess again!");
            tries++;
            int guess = scan.nextInt();     
        }
        System.out.println("You have figured it out in " + tries + " tries.");}
    }
} 

I created a variable called guess outside the while loop. When I try to change guess inside of the loop, it says that guess is a "duplicate local variable".

It says "duplicate variable" because you declared it twice:

int guess = scan.nextInt();
//...
while (guess != num) {
    //...
    int guess = scan.nextInt();     
}

Remove the modifier:

int guess = scan.nextInt();
//...
while (guess != num) {
    //...
    guess = scan.nextInt();     
}

Potentially an easier solution altogether:

int guess;
while ((guess = scan.nextInt()) != num) {
    //do code
}

It does have a duplicate local variable. Here it is:

int guess = scan.nextInt();

Change this to:

guess = scan.nextInt();

And it will work.

What was wrong?

When you have a type name in front of a variable (eg int guess or Object foo ), that is a declaration in Java. And it will hide a previous declaration. See the below example:

int guess = 0; // we declare an int, named guess, and at the same time initialize it

for (...) { // with this curly braces, a new local scope starts
    // NOTE: in fact, curly braces start a new scope. the curly braces could be
    // there on it's own too. it does not have to be associated with a 
    // 'for', or a 'while', or a 'do-while', or an 'if'
    guess = 5; // this refers to the variable outside the scope, and overwrites it.

    // here be dragons!
    int guess = 2; // we're declaring an int, named 'guess' again. this will hide the former.
    guess = 8; // this refers to the locally declared variable, as the former one is hidden by it now
}

System.out.println(guess); // should print 5

You declared guess outside of the loop already ( int guess = scan.nextInt(); ). You're trying to declare it again inside the loop, thus you are getting the message for "duplicate local variable."

You should remove the declaration inside the loop so that your loop looks like this:

        int guess = scan.nextInt();
        // ...
        while (guess != num) {
            // ...
            tries++;
            guess = scan.nextInt();
        }
        // ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM