Java-Can';不要打破循环
无法跳出while循环。我的代码有什么问题? 在我看来,逻辑上一切都是正确的: While(不是's'或'p') ---打印行:“重新键入” ---读取输入:Java-Can';不要打破循环,java,loops,while-loop,Java,Loops,While Loop,无法跳出while循环。我的代码有什么问题? 在我看来,逻辑上一切都是正确的: While(不是's'或'p') ---打印行:“重新键入” ---读取输入: while (!answerInput.equals("s") || !answerInput.equals("p")) { System.out.print("Wrong Input. Type S for Sum or P for Product: "); answerInput = userInput.next();
while (!answerInput.equals("s") || !answerInput.equals("p"))
{
System.out.print("Wrong Input. Type S for Sum or P for Product: ");
answerInput = userInput.next();
answerInput = answerInput.toLowerCase();
}
请告知。用简单的英语,您的代码是: 如果“answerInput”不是“s”,“answerInput”不是“p”,则此表达式为真 这句话永远不会是假的。很明显不是“s”就是“p”;它不能同时是两个值。两个不等式中的一个总是真的,因此整个表达式也总是真的 你应该说的是: 如果“answerInput”不是“s”,“answerInput”也不是“p”,则此表达式为真 在Java中表示为:
while (!answerInput.equals("s") && !answerInput.equals("p"))
如果你说While(不是“s”或“p”),你的错误是可以理解的。但是While(不是's'或'p')有什么意义呢?谢谢你。看起来并不是每件事都符合我的逻辑。必须画出所有可能的真/假组合,看看我错在哪里。我希望它现在能进入我的脑海。