2014-09-28 59 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