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

ねえ、また助けを求めて戻ってきて。

while ステートメントは、ユーザーが b または c または A 以外の値を入力すると意図したとおりに機能しますが、A を入力すると while ループから抜け出しますが、「間違った選択をした」という文字列が出力されます。理由はわかりませんが、理由を教えていただけると確信しています。

さらに、正しい番号を入力すると、2 番目のループは完全に機能します。唯一の問題は、そうしないと、「コンボが正しくありません」と表示されますが、一度印刷するだけでなく、印刷を続けて停止しないことです。その終わりのない。私は何を間違えましたか?

多分私はif文を使うべきですか?え…いや…ループしない…うーん。

ps最後の数字は5ではないと言ったことは知っていますが、投稿で修正してください

4

2 に答える 2

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」に一致する場合、ループを壊します

2 番目の問題については、まったく同じアプローチを使用できます。これは、ロジックが同じであるためです。期待どおりになるまで無限にループします。

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

キーの最初のステートメントを毎回繰り返す場合は、それを 2 番目の while の最初に入れます。ゲーム制作頑張ってください!念のため、私が書いた作業コードの要点を次に示し ます https://gist.github.com/dallarosa/14617052520c571ad2ad

于 2014-09-28T06:47:20.297 に答える
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");
       }
}

さて、2番目の部分では、これが私がやったことです。これにより、あなたが説明した問題も修正されました。

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 に答える