简体   繁体   English

布尔输入到字符串yes为do循环中的wantToContinue

[英]boolean input to string yes for wantsToContinue in do while loop

I am trying to use a do while loop that will terminate if the user answers yes to "do you want to continue" If I answer true in the console the loop functions properly, but, I would like to tell the user to input yes or no. 我正在尝试使用do while循环,如果用户对“您要继续吗”回答“是”,该循环将终止,如果我在控制台中回答“是”,则循环功能正常,但是,我想告诉用户输入“是”或“没有。 So, I searched for info on how to assign a string value to a boolean value of true but my code is not working. 因此,我搜索了有关如何将字符串值分配给boolean值true的信息,但是我的代码无法正常工作。 Can someone show me the proper syntax? 有人可以告诉我正确的语法吗?

boolean wantsToContinue=String.equalsIgnoreCase("yes");

do{
    rollOne=diceOne.roll();
    rollTwo=diceTwo.roll();
    rollTotal=rollOne+rollTwo;
    //BoardSquare thePlayer.setLocation(square[]);

    Scanner keyboard=new Scanner(System.in);
    System.out.println("Do you want to continue:");
    wantsToContinue = keyboard.nextBoolean();

} while (thePlayer.getBalance()>0 && wantsToContinue);

When I set it up this way I get non static method cannot be referenced from a static context. 以这种方式设置它时,我无法从静态上下文引用非静态方法。 I need to avoid having a separate method or object for this logic. 我需要避免为此逻辑使用单独的方法或对象。

try this 尝试这个

    String wantsToContinue="";

    do{

        rollOne=diceOne.roll();
        rollTwo=diceTwo.roll();
        rollTotal=rollOne+rollTwo;
        //BoardSquare thePlayer.setLocation(square[]);


        Scanner keyboard=new Scanner(System.in);
        System.out.println("Do you want to continue:? yes | no");
        wantsToContinue = keyboard.next();

    } while (thePlayer.getBalance()>0 && wantsToContinue.equalsIgnoreCase("yes"));

In each iteration prompt for yes or no. 在每个迭代提示中,选择是或否。 if you give yes the loop will continue(also based on your getBalance return >0). 如果您给出是,则循环将继续(同样基于您的getBalance返回> 0)。 or it is terminate. 否则终止。 equalsIgnoreCase("yes") it is check given option yes or YES or its combination. equalsIgnoreCase(“ yes”) ,检查是否为选项yesYES或其组合。 if equal it return true. 如果相等,则返回true。

equalsIgnoreCase is not declared as static method in String class, but you are using it as String.equalsIgnoreCase . 在String类中, equalsIgnoreCase没有声明为静态方法,但是您将其用作String.equalsIgnoreCase Hence the error "non static method cannot be referenced from a static context" 因此,错误“无法从静态上下文引用非静态方法”

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

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