简体   繁体   English

Eclipse无法找到变量

[英]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. 我对Java比较陌生(准确地说是2天),并且正在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. numberofTriesnumberOfTries ,因为“ of”中的O不大写。

In your last line there is a simple typo; 在最后一行,有一个简单的错字; that numberofTries should be numberOfTries (case sensitivity) 该numberofTries应该是numberOfTries(区分大小写)

Java is case sensitive! Java区分大小写!

System.out.println("It took you " + numberofTries + "tries"); System.out.println(“花费了您” + 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. 在第35行,您有一个未定义的名为“ numberofTries”的变量。 As others have noted, the O in 'of' needs to be capitalized because Java is case-sensitive (as are most programming languages). 正如其他人指出的那样,“ of”中的O in必须大写,因为Java区分大小写(与大多数编程语言一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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