【问题标题】:My boolean variable could not be resolved into a variable in my while statement我的布尔变量无法解析为我的 while 语句中的变量
【发布时间】:2019-02-16 22:43:20
【问题描述】:

我想制作一个重复自己的代码,直到用户输入单词 Game 或单词 Balance。我做了一个do while 循环,但我的while 语句出错。错误是:error3 cannot be resolved into a variable。有谁知道我的代码有什么问题?

System.out.println("welcome to Roll the Dice!");
System.out.println("What is your name?");

Scanner input = new Scanner(System. in );
String Name = input.nextLine();

System.out.println("Welcome " + Name + "!");
System.out.println("Do you want to play a game or do you want to check your account's balance?");
System.out.println("For the game typ: Game. For you accounts balance typ: Balance");

do {
    String Choice = input.nextLine();
    String Balance = "Balance";
    String Game = "Game";
    input.close();

    boolean error1 = !new String(Choice).equals(Game);
    boolean error2 = !new String(Choice).equals(Balance);
    boolean error3 = (error2 || error1) == true;

    if (new String(Choice).equals(Game)) {
        System.out.println("Start the game!");
    }

    else if (new String(Choice).equals(Balance)) {
        System.out.println("Check the balance");
    }

    else {
        System.out.println("This is not an correct answer");
        System.out.println("Typ: Game to start a game. Typ: Balance to see your account's balance");
    }
}
while ( error3 == true );

【问题讨论】:

    标签: java if-statement boolean do-while


    【解决方案1】:

    error3do 范围内定义。将其声明移到do 范围之外并在其中设置值:

        boolean error3 = false;
        do {
            String Choice = input.nextLine();
            String Balance = "Balance";
            String Game = "Game";
            input.close();
    
            boolean error1 = ! new String(Choice).equals(Game);
            boolean error2 = ! new String(Choice).equals(Balance);
            error3 = error2 || error1; 
    

    另请注意,您可以将(error2 || error1) == true 简化为error2 || error1。您的while 声明也可以这样做:

    while(error3);
    

    【讨论】:

    • 您好,谢谢您的回答。但是我仍然收到一个错误,我更改了我的代码,但他目前给了我另一个错误。在该行:布尔error3 = error2 ||错误1;错误是:链接所有引用以进行本地重命名(不更改其他文件中的引用)。您是否也知道此错误的解决方案?谢谢:)
    • 错误截图?听起来像是来自 IDE 的重构建议,而不是真正的错误。
    • 其实大卫below是对的。 error3 = error2 || error1,您不想在该行的开头使用 boolean
    【解决方案2】:

    您正试图在声明它的循环体范围之外读取 error3 的值:

    while(error3 == true);
    

    只需在循环前声明即可:

    boolean error3 = true;
    do {
        //...
        error3 = (error2 || error1) == true; 
        //...
    }
    while(error3 == true);
    

    【讨论】:

    • 您好,谢谢您的回答。但是我仍然收到一个错误,我更改了我的代码,但他目前给了我另一个错误。在该行: boolean error3 = (error2 || error1) == true;错误是:链接所有引用以进行本地重命名(不更改其他文件中的引用)。当我运行代码 Eclipse 说:重复的局部变量。您是否也知道此错误的解决方案?谢谢:)
    • @StijnKrabbenborg:因为您试图在循环内重新声明 error3 变量。不要重新声明它,像我在答案中那样使用现有变量。
    【解决方案3】:

    因为您在 do-while 循环中定义了变量。 这样做:

    boolean error3=true;
    do {
            String Choice = input.nextLine();
            String Balance = "Balance";
            String Game = "Game";
            input.close();
    
            boolean error1 = ! new String(Choice).equals(Game);
            boolean error2 = ! new String(Choice).equals(Balance);
            error3 = (error2 || error1) == true; 
    
            if (new String(Choice).equals(Game)){
                System.out.println("Start the game!");
            }
    
            else if(new String(Choice).equals(Balance)){
                System.out.println("Check the balance");
            }
    
            else{
                System.out.println("This is not an correct answer");
                System.out.println("Typ: Game to start a game. Typ: Balance to see your account's balance");
                }
        } 
        while(error3 == true);
    

    【讨论】:

    • 您好,谢谢您的回答。但是我仍然收到一个错误,我更改了我的代码,但他目前给了我另一个错误。在该行: boolean error3 = (error2 || error1) == true;错误是:链接所有引用以进行本地重命名(不更改其他文件中的引用)。当我运行代码 Eclipse 说:重复的局部变量。您是否也知道此错误的解决方案?谢谢:)
    • 从循环内的变量中移除布尔类型
    • 接受的答案可以帮助其他人快速找到解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2014-03-30
    • 2016-01-22
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多