while(!A){
System.out.println ("You enter a room and look around, in it, you see three doors, a red door labeled A, a blue door labeled B, and a green door labeled C. Which door do you choose to go through? Enter, A, B, or C");
String correctdoor = scanner.next();
A = "A".equalsIgnoreCase(correctdoor);
System.out.println("You have chosen wrong! You have fallen into a pit! Lucky for you though, the pit is easy to climb out of and you return to the room....\n\n\n");
}
System.out.println("You progress through the door and find 5 light switches, you must turn them on in order to progress further. Enter the correct combination (using digits 1-5) here please. HINT - the 2nd and 4th numbers add up to the first number and the last number is NOT 5");
int lightcode = scanner.nextInt();
while (!(lightcode == 31425)){System.out.println ("That combination is incorrect");}
System.out.println ("The door unlocks and you go down a set of stairs");
嘿,再次回来寻求更多帮助。
当用户输入 b 或 c 或任何其他不是 A 的值时,while 语句确实按预期工作,但是当他们输入 A 时,它确实将它们带出 while 循环,但它仍然打印“你选择了错误”字符串。我不知道为什么,但我相信你们可以告诉我为什么。
此外,如果我输入正确的数字,第二个循环可以完美运行,唯一的问题是,当我不输入时,它会告诉我“组合不正确”,但它不会只打印一次,它会继续打印并且不会停止,它永无止境。我做错什么了?
也许我应该使用 if 语句?呃……不……那不会循环……呃。
ps 我知道我说最后一个数字不是 5,但我会在帖子中修复它