更多关于基于文本的游戏的问题

时间:2014-09-28 05:36:05

标签: java boolean text-based

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,但在帖子

中修复了它

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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");
}

我明白intui!(lightcode == 31425)tively,在while循环中设置条件看起来是正确的方法,但问题是,这个条件有点无用,因为你有流量控制发生在while循环。所以,我只是更清楚地说明了:永远保持循环,如果corredoor匹配“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");

如果你是每次重复键的第一个声明,只需将它放在第二个的开头。 祝你的游戏制作好运! 为了以防万一,这里是我写的工作代码的要点 https://gist.github.com/dallarosa/14617052520c571ad2ad