无法弄清楚如何在Java博士中为我的Java程序做一次尝试捕获异常?

时间:2015-01-20 22:55:16

标签: java try-catch drjava

所以,我希望Java在输入的内容之外捕获除了指定的内容之外但我无法弄清楚如何正确尝试捕获异常。有时它只是跳到程序的末尾,或者在这种情况下,我只是出错了。如果你能提供帮助那就很好,因为我需要明天完成这个项目。对不起,通知时间紧迫。

//Step 1: Import Java APIs
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;




//Step 2: Name File and Class

public class GolfScores {



    //Step 3: Declare All Variables  

    public static void main(String[] args) {
       int hole9 = 0;
       int hole18 = 0;
       int holeChoice = 0;
       int albetross = 0;
       int eagle = 0;
       int birdie = 0;
       int par = 0;
       int boogie = 0;
       int boogie2 = 0;
       int holeinone = 0;
       int score = 0;
       int errorinput = 0;


       Scanner input = new Scanner(System.in);


       //Step 4: The program accepts INPUT from the user

       try {
           System.out.println("For hole 9, please enter 9. If you are on hole 18,          please enter 18 ");
           holeChoice = input.nextInt();

           if (holeChoice == 9) {
               System.out.println("The par for this hole is 3. Please enter if you this hole took you: 1, 2, 3, 4, 5, or more shots");
               hole9 = input.nextInt();

           } else if (holeChoice == 18) {
               System.out.println("The par for this hole is 5. Please enter if you this hole took you: 1, 2, 3, 4, 5, 6, 7, or more shots");
               hole18 = input.nextInt();
           }

       } catch ( InputMismatchException inputMismatchException)
       {
           System.err.printf ("\nException: %s\n",
                     inputMismatchException );

           System.out.println ("Please enter a valid number, either 9 or 18");
       }

       errorinput = input.nextInt();




      //Step 5 & 6: The user input is PROCESSED by Java and uses math to   calcualte an answer to output. The user's score is then output for them to see.
       if (hole18 == 1) {
           System.out.println("Your score for this hole was a hole in one!");

       } else if (hole18 == 2) {
           System.out.println("Your score for this hole was albetross.");

       } else if (hole18 == 3) {
           System.out.println("Your score for this hole was eagle.");

       } else if (hole18 == 4) {
           System.out.println("Your score for this hole was birdie.");

       } else if (hole18 == 5) {
           System.out.println("Your score for this hole was par.");

       } else if (hole18 == 6) {
           System.out.println("Your score for this hole waspboogie.");

       } else if (hole18 == 7) {
           System.out.println("Your score for this hole was double boogie.");

       } 


       if (hole9 == 1) {
           System.out.println("Your score for this hole was a hole in one!");

       } else if (hole9 == 2) {
           System.out.println("Your score for this hole was birdie.");

       } else if (hole9 == 3) {
           System.out.println("Your score for this hole was par.");

       } else if (hole9 == 4) {
           System.out.println("Your score for this hole was boogie.");

       } else if (hole9 == 5) {
           System.out.println("Your score for this hole was double boogie.");

       }

   }

}  

这是错误:

Exception: java.util.InputMismatchException
Please enter a valid number, either 9 or 18
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)


at java.util.Scanner.nextInt(Unknown Source)
at GolfScores.main(GolfScores.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at     edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  

因此它可以输出正确的语句,但是如何在此之后继续该程序?我还需要其余的输入,但如果有人可以告诉我如何为第一部分做,我想我可以弄清楚其余的。谢谢。

2 个答案:

答案 0 :(得分:1)

try-catch放入循环中。像

这样的东西
for (;;) {
    try {
        System.out.println("For hole 9, please enter 9. "
                + "If you are on hole 18, please enter 18 ");
        holeChoice = input.nextInt();
        if (holeChoice == 9) {
            System.out.println("The par for this hole is 3. " //
                        + "Please enter if you this hole took you: "
                        + "1, 2, 3, 4, 5, or more shots");
            hole9 = input.nextInt();
        } else if (holeChoice == 18) {
            System.out.println("The par for this hole is 5. " //
                        + "Please enter if you this hole took you: "
                        + "1, 2, 3, 4, 5, or more shots");
            hole18 = input.nextInt();
        } else {
            System.out.println("Please enter a valid number, either 9 or 18");
            continue;
        }
        break;
    } catch (InputMismatchException inputMismatchException) {
        System.err.printf("\nException: %s\n", inputMismatchException)
        System.out.println("Please enter a valid number, either 9 or 18");
       input.next(); // consume bad token
    }
}

答案 1 :(得分:-1)

您的errorinput = input.nextInt();不在try区块之内。这就是为什么你无法抓住它。你可能需要另一个try-catch块。