2017-01-23 80 views
-3

嘿傢伙我需要幫助爲我的實驗練習。我只需要通過這個問題..我唯一的問題,我需要確保將有一個例外,如果用戶試圖輸入一個數字(特別是int)在一個字符串「但** 我使用java.util.Scanner和讀取輸入的String條件陳述如果用戶輸入整數而不是字符串

else if (userInput.hasNextInt())我的問題只有

保存爲圖像 enter image description here

import java.util.*; 

public class Quizbee{ 

    public static void main (String args[]){ 

     Scanner sc = new Scanner(System.in); 
     int score = 0; 
     String userInput; 
     String answerKey[] = {"A", "C", "B", "B", "C"}; //A, C, 

     String multichoice[][] = {{"A = Leonardo Da Vinci ", "B = Albert Einstein ","C = Stephen Hawking"}, //[0][0],[0][1],[0][2] 
            {"A = 9 ", "B = 8 ", "C = 7 "},           //[1][0],[1][1],[1][2] 
            {"A = Rodrigo Duterte ", "B = Ferdinand Marcos ", "C = Ninoy Aquino "}, //[2][0],[2][1],[2][2] 
            {"A = John F. Kennedy ","B = Abraham Lincoln ","C = Ronald Reagan "},  //[3][0],[3][1],[3][2] 
            {"A = Floyd Mayweather ","B = Manny Pacquaio ", "C = Muhammad Ali "}};  //[4][0],[4][1],[4][2] 
             {} 

     String arrQuestion[] = {"The Renaissance Man?", "prime number?","Longest-term serving Pres. of PH", 
           "1st US President to be assassinated", "Nicknamed \"The Greatest\" in the boxing history"}; 


      for (int x = 0; x<arrQuestion.length; x++) 
      { 
       try 
       { 
        System.out.println("\n" + (x+1)+". " + arrQuestion[x]); 
        System.out.print(multichoice[x][0] + multichoice[x][1] + multichoice[x][2]+": "); 
        userInput = sc.nextLine(); 


        if(userInput.equalsIgnoreCase(answerKey[x])) 
        { 
         System.out.println("Correct!"); 
         score = score + 1; 
        } 

        else if(!userInput.equalsIgnoreCase(answerKey[x])) 
        { 

         if(userInput.isEmpty()) 
         { 
          throw new NullPointerException(); 
         } 

         else if(!userInput.equalsIgnoreCase("A") && !userInput.equalsIgnoreCase("B") && !userInput.equalsIgnoreCase("C")) 
         { 
          throw new OutOfRangeMisception(); 
         } 

         else if(userInput.hasNextInt()) 
         { 
          throw new InputMismatchException(); 
         } 

         else 
         { 
          System.out.println("Wrong!"); 
          score = score + 0; 
         } 
        } 
       } 

       catch(OutOfRangeMisception oor) 
       { 
        System.out.println(oor.getMessage());  
       } 

       catch(NullPointerException ex) 
       { 
        System.out.println("No answer. please try again.");  
       } 
       catch(InputMismatchException ex) 
       { 
        System.out.println("Wrong data type."); 
       } 
      } 
     System.out.println("\nYour score: " + score); 
    } 
} 
+0

你的問題是什麼?還是你要求我們爲你做作業? – CraigR8806

+0

歡迎,除了實驗室要求,我沒有看到具體的問題。即使有一個不好的英語(像我的),你可以專注於這個問題,並創建一個[mcve] – AxelH

+1

而不是'userInput.hasNextInt()'我會建議'userInput.matches(「\\ d +」)''。它檢查字符串是否包含數字。 –

回答

0

你可以使用Integer.parseInt(userInput)實驗練習。如果成功,用戶輸入一個整數,OTH否則你會得到一個異常。所以,如果它是一個整數,你可以拋出你的異常,否則你趕上NumberFormatException

0

你可以使用這個簡單的方法來測試,如果String是數量還是不

public boolean isNumber(String text) { 
    try { 
     Integer.parseInt(text); 
     return true; 
    } catch (NumberFormatException exception) { 
     return false; 
    } 
} 
0

如果你使用不僅僅是Java,用StringUtils.isNumeric()

檢查如果字符串包含unicode的唯一數字或空格('')。小數點不是unicode數字,並返回false。

null將返回false。一個空字符串(length()= 0)將返回true。

所以你的情況,你會使用:

else if(StringUtils.isNumeric(userInput)) 

注意,這將在Android中並不工作。相反,你需要TextUtils.isDigitsOnly()