2014-09-28 60 views
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,但衣衫将其固定后

+0

为什么不输入''A'选择了错误!为什么第二个'while'不会无限循环? – 2014-09-28 05:37:12

+0

,因为A是正确的答案,它将它们带出循环。 – METEORITES 2014-09-28 05:38:34

+0

您如何看待代码执行?如果一个声明在另一个声明之后出现,是否应该在其他声明之后执行? – 2014-09-28 05:40:14

回答

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"); 
+0

所以我必须使用if语句!至少我是在正确的轨道上,非常感谢你!这些工作奇妙,现在我明白我犯了我的错误。 – METEORITES 2014-09-28 06:20:46

+0

@METEORITES没问题 – Shadow 2014-09-28 06:23:29

+0

@METEORITES另外,我能否让您将该答案标记为已接受? – Shadow 2014-09-28 06:31:12

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循环中。所以,我只是更加清楚地说明了这一点:永久循环,如果correctdoor匹配“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

+1

嘿...我喜欢这样看起来!非常感谢你帮助我!我学到了很多东西,我知道有足够的基础知识来开始这个游戏,但我遇到了一些完善我的循环的问题。这种方式看起来很容易记住,我可能会在项目的其余部分使用它。 – METEORITES 2014-09-28 07:47:32