简体   繁体   中英

Eclipse Unable To Find Variable

I'm relatively new to Java (2 days to be exact) and I'm writing a guessing game in Eclipse. Every time I run it in the console I get this:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
numberofTries cannot be resolved to a variable

at GuessingGame.main(GuessingGame.java:35)

I'm following a tutorial and this is what my code is:

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {
    public static void main(String[] args) {
        Random rand = new Random();
        int numberToGuess = rand.nextInt(1000);
        int numberOfTries = 0;
        Scanner input = new Scanner(System.in);
        int guess;
        boolean win = false;

        while (win == false) {

            System.out.println("Guess a number between 1 and 1000: ");
            guess = input.nextInt();
            numberOfTries++;

            if (guess == numberToGuess) {
                win = true;
            }

        }
        if (guess < numberToGuess) {
            System.out.println("Your guess is too low");

        }
        else if (guess > numberToGuess) {
            System.out.println("Your guess is too high");
        }
        System.out.println("You win!");
        System.out.println("The number was " + numberToGuess);
        System.out.println("It took you " + numberofTries + "tries");
    }
}

I double checked everything and everything was correct.

Capitalization issue. numberofTries is not the same as numberOfTries , as the O in "of" isn't capitalized.

In your last line there is a simple typo; that numberofTries should be numberOfTries (case sensitivity)

Java is case sensitive!

System.out.println("It took you " + numberofTries + "tries");

Your variable name in this line is not the same as the variable you created, you probably just misstyped :)

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
numberofTries cannot be resolved to a variable

at GuessingGame.main(GuessingGame.java:35)

Use these error messages as guidance. It's quite literally telling you exactly what's going on.

At line 35, you have a variable named 'numberofTries' that's undefined. As others have noted, the O in 'of' needs to be capitalized because Java is case-sensitive (as are most programming languages).

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