2017-02-26 5 views
3
public void choice(){ 

    this.damageCalculator(); 
    story.typeWriterEffect("Give your choice of attack: \n"); 

      while (!input.hasNextInt()){ 
       story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n"); 
       story.typeWriterEffect("Give your choice of attack: \n"); 
       input.reset(); 
       input.next(); 
      } 
      setUserChoiceOfAttack(input.nextInt()); 

     if(getUserChoiceOfAttack() > 0 && getUserChoiceOfAttack() < 4){ 
      this.userAttackMoment(); 
     } else { 
      story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n"); 
      this.choice(); 
      } 
} 

問題促したい: をあなたが入力で「笑ハズレ」を入力すると、それは出力が「入力してくださいます... "と"あなたの選択を... ... "を2回入力します(空白で区切られたすべての単語に対して) それはすべてうまく動作しますが、私を悩ます事は、ユーザーが何かを入力すると、彼はいけない、ここでプログラマーを開始します。()whileループでは、与えられたすべての文字列のループを与えるが、私はそれが一度

(それは複数のスペースで区切られた文字列をだとしても)、私のコードの書き込み上の任意のフィードバックは大歓迎です!

EDIT:

pivo0のコメントはうまくいった! これは私の固定コードであり、完璧に動作します!

public void choice(){ 

    this.damageCalculator(); 

     story.typeWriterEffect("Give your choice of attack: \n"); 

     boolean loopCondition = true; 
     while(loopCondition == true){ 

      try{      
       player.setPlayerChoiceOfAttack(Integer.parseInt(input.nextLine())); 

       if(player.getPlayerChoiceOfAttack() > 0 && player.getPlayerChoiceOfAttack() < 4){ 
        loopCondition = false; 
       } else { 
        story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n"); 
        } 
      } 
      catch (NumberFormatException e){ 
       loopCondition = true; 
       story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n"); 
       story.typeWriterEffect("Give your choice of attack: \n"); 
      } 
     } 
     this.userAttackMoment(); 
} 

答えて

1

をチェックするブール変数を持っている私は、入力は、使用している方法によるScannerのインスタンスであることを前提としています。

あなたはhasNextInthttps://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

のドキュメントを読む場合は、次のトークンはintで天気をチェックし、それがいることがわかります。スキャナークラスの場合、トークンはスペースで区切られます。あなたの最初の単語をチェックして、 '笑'がIntではないと結論し、ループを通り、 'nop'がIntでなく、再び谷のループになると結論づけます...

文字列としてinput.nextLine()を使用して、この文字列が整数かどうかを確認してください。Integer.parseInt

input.nextLine()が整数の場合、ループから外れます。そうでない場合は、新しい入力が確認されます:

int number=0; 
boolean loopCondition = false; 
while(!loopCondition){ 
try { 
    number = Integer.parseInt(input.nextLine()) 
    loopCondition = true; 
    } 
catch (NumberFormatException e){ 
    story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n"); 
      story.typeWriterEffect("Give your choice of attack: \n"); 
    loopCondition = false; 
} 
} 
+0

ありがとうございます! 作品:D !!!! – Icezman

1

が試み

//attempt is initally false 
public void choice(boolean attempt){ 

    this.damageCalculator(); 

     story.typeWriterEffect("Give your choice of attack: \n"); 

      while (!input.hasNextInt()){ 
       story.typeWriterEffect("Please enter the correct number (1, 2 or 3).(ERROR NO INT)\n"); 
       story.typeWriterEffect("Give your choice of attack: \n"); 
       input.reset(); 
       input.next(); 
      } 
       setUserChoiceOfAttack(input.nextInt()); 

     if(getUserChoiceOfAttack() > 0 && getUserChoiceOfAttack() < 4){ 
      this.userAttackMoment(); 
     } else if(!attempt){ 
      story.typeWriterEffect("Please enter the correct number (1, 2 of 3).(ERROR INPUT INVALID)\n"); 
      this.choice(true); 
      } 
} 
関連する問題