0
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,但我会在帖子中修复它

4

2 回答 2

0

我查看了您的代码,并想出了这个,它对我有用并解决了您描述的问题:

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);

       if (!A) // Added this here, displays the message only if they chose the incorrect door
       {
           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");
       }
}

现在,对于第二部分,这就是我所做的,这也解决了您描述的问题:

int lightcode = 0; //Initialize lightcode to something incorrect here
while (!(lightcode == 31425))
{
 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");   
    lightcode = scanner.nextInt(); //Get lightcode from the player

    if (!(lightcode == 31425)) //And finally, only if the code is INCORRECT, display the incorrect message
    {
     System.out.println ("That combination is incorrect");
    }
}

System.out.println ("The door unlocks and you go down a set of stairs");
于 2014-09-28T05:59:17.463 回答
0

好的,这就是我的做法:

while(true){ 
  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();
  if("A".equalsIgnoreCase(correctdoor)) {
    break;
  }
  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");
}

我理解直觉!(lightcode == 31425)在while循环中设置条件看起来是正确的方法,但问题是,该条件有点无用,因为您在while循环内部进行了流量控制。所以,我只是更清楚地说明了这一点:永远循环,如果正确的门匹配“A”打破循环

对于您的第二个问题,您可以使用相同的方法,因为逻辑是相同的:永远循环,直到您得到您所期望的。

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 = 0;
while (true){
  lightcode = scanner.nextInt();
  if(lightcode == 31425) {
    break;
  }
  System.out.println ("That combination is incorrect");
}
  System.out.println ("The door unlocks and you go down a set of stairs");

如果您每次都重复键的第一个语句,只需将其放在第二个 while 的开头即可。祝您游戏制作顺利!以防万一,这是我编写的工作代码的要点 https://gist.github.com/dallarosa/14617052520c571ad2ad

于 2014-09-28T06:47:20.297 回答