2017-01-23 11 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

あなたがStringUtils.isNumeric()使い、あなただけのJavaを使用している場合Stringが数か

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

であるかどうかをテストするために、この単純な方法を使用することができます:文字列はUnicodeのみが含まれている場合

のチェックを数字またはスペース( '')。小数点はUnicode数字ではなく、falseを返します。

nullはfalseを返します。空のString(length()= 0)はtrueを返します。

だからあなたの場合には、あなたが使用したい:

else if(StringUtils.isNumeric(userInput)) 

注意このは、Androidで動作しないこと。代わりにあなたが必要ですTextUtils.isDigitsOnly()

関連する問題