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();
}
}
問題: 如果輸入「大聲笑沒了」在輸入端則輸出「請輸入...「和」給你的選擇......「兩次(每個單詞都用空格鍵入和分隔) 這一切都運行良好,但有一件事情是我只想讓它重複一次,如果用戶進入一些東西他不應該(即使它是由空格分隔的多個字符串)。隨着hasNextInput()在while循環它給給定每個字符串的循環,但我想它提示一次
從這兒開始的程序員和我的代碼編寫任何反饋意見表示讚賞!
編輯:
pivu0的評論算出來了! 這是我的固定代碼,完美的作品!
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