1
public void humanPlay()
 {
if (player1.equalsIgnoreCase("human"))
    System.out.println("It is player 1's turn.");
else
    System.out.println("It is player 2's turn.");

System.out.println("Player 1 score: " + player1Score);
System.out.print("Player 2 score: " + player2Score);

String eitherOr;

  do {
    eitherOr= input.nextLine(); 
    humanRoll();
  } while (eitherOr.isEmpty());

 if (!eitherOr.isEmpty())
    humanHold();

}

これが全体の方法です。私が修正しようとしているのはこれだけです。

       String eitherOr;
do {
     eitherOr= input.nextLine();    
     humanRoll();
   } while (eitherOr.isEmpty());

入力を複数回受け入れる必要があるため、何が起こるかを判断するために毎回入力が必要です。そのため、Do Whileループが好きですが、少なくとも1回に1回初期化されるため、必要以上に余分なロールが発生します。

私はこの方法でそれをやろうとしました、そしてこの方法のさまざまなバリエーション:

String eitherOr = input.nextLine();

while(eitherOr.isEmpty());
        humanRoll();

入力を再度要求しないため、これは機能しません。input.nextline();を入れようとすると whileループに入ると、「etherOr」は初期化されていないと表示されます。入力を入力したときに初期化しても、コマンドラインは空白のままなので、入力には何の影響もありません。

4

2 に答える 2

4

あなたは無関係なセミコロンを持っています:

while(eitherOr.isEmpty());
    humanRoll();'

する必要があります:

while(eitherOr.isEmpty())
    humanRoll();

eitherOr.isEmpty()基本的に、あなたのバージョンは、ある間は何もしないと言っているtrueので、を呼び出すことはありませんhumanRoll

于 2011-12-12T04:15:03.970 に答える
1

2番目のコードスニペットがwhileループの一部として空白のステートメントを実行している場合

while(eitherOr.isEmpty());//this semicolon is a blank statement
    humanRoll();

ループの一部としてhumanRollを実行するには、このセミコロンを削除する必要があります

while(eitherOr.isEmpty())
    humanRoll();

ちなみに、paranthesisを使用すると、通常、このような小さな問題を回避できます。

while(eitherOr.isEmpty()) {
    humanRoll();
}

上記のコードでは、意図しないセミコロンが導入されているかどうかを簡単に識別できます。

于 2011-12-12T04:22:22.760 に答える