//我需要在用户键入y OR n。时打破while循环,但是到目前为止,我只能用一个字母/字符串来获取它。
Scanner user_input = new Scanner(System.in);
String name = user_input.next();
System.out.print("Would you like to order some coffee, "+name+"? (y/n)");
String coffeeYorN = user_input.next();
while (!coffeeYorN.equals("y")||!coffeeYorN.equals"n") //HERE IS MY ISSUE
{
System.out.println("Invalid response, try again.");
System.out.print("Would you like to order some coffee, "+name+"? (y/n)");
coffeeYorN = user_input.next();
}
最佳答案
当条件为真时,执行此循环。
假设有人输入“ n” ...
您的条件说:Is the input something other than "y"? Yes, it is "n", so I should execute the loop.
您需要这样的内容:while(!coffeeYorN.equals("y") && !coffeeYorN.equals("n"))