【问题标题】:Can't Figure Out How To Do a Try Catch Exception for my Java Program in Dr. Java?无法弄清楚如何在 Dr. Java 中为我的 Java 程序尝试捕获异常?
【发布时间】:2015-01-20 22:55:15
【问题描述】:

所以,我希望 Java 在输入的内容不是指定的内容时捕获,但我不知道如何执行正确的 try catch 异常。有时它只是跳到程序的末尾,或者在这种情况下,我只是得到错误行。如果你能帮忙那就太好了,因为我明天需要完成这个程序。很抱歉通知和压力。

//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)

所以它的工作原理是它输出了正确的语句,但是在此之后我怎样才能继续程序呢?其余的输入我也需要它,但如果有人能告诉我如何在第一部分做它,我想我可以弄清楚其余的。谢谢你。

【问题讨论】:

  • Elliot Frisch 的回答是一个很好的建议,但是您还在 try/catch 块之后处理了输入,该块为 errorinput 分配了一个值,该块随后从未使用过。您的程序能够继续,但由于您没有将任何变量设置为有效值,因此您看不到任何结果。
  • 您可以使用业内众所周知的“循环”。

标签: java try-catch drjava


【解决方案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
    }
}

【讨论】:

  • 当我这样做时,for循环无限返回: System.err.printf("\nException: %s\n", inputMismatchException); System.out.println("请输入有效数字,9 或 18");声明
  • 你是个才华横溢的神秘电脑人。现在,这会处理输入的字符串,如果我在输入中输入了不正确的数字,它只会将该数字显示为 Java 的输出并且程序仍然运行。如何获取 System.out.println("请输入有效数字,9 或 18");输入错误的数字时弹出?
  • @user4476009 再次编辑。添加else 以显示您的消息。下次,请将您的所有预期行为添加到您原来的问题中。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2021-12-13
    • 2015-11-13
    • 2011-04-20
    • 2020-11-14
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多