2017-02-26 61 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(); 
      } 
} 

問題: 如果輸入「大聲笑沒了」在輸入端則輸出「請輸入...「和」給你的選擇......「兩次(每個單詞都用空格鍵入和分隔) 這一切都運行良好,但有一件事情是我只想讓它重複一次,如果用戶進入一些東西他不應該(即使它是由空格分隔的多個字符串)。隨着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(); 
} 

回答

1

我認爲輸入的Scanner您所使用的方法的實例。

如果你讀的hasNextInthttps://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

的文檔,你看到它檢查閹羊的下一個標記是int。對於掃描儀類,令牌由空格分隔。所以它會檢查你的第一個詞,得出結論'lol'不是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); 
      } 
} 
相關問題