2017-01-23 65 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()