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();
}
ありがとうございます! 作品:D !!!! – Icezman