因此,我希望 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)
所以它的工作原理是输出正确的语句,但是在此之后我该如何继续程序呢?其余的输入我也需要它,但如果有人能告诉我如何在第一部分做它,我想我可以弄清楚其余的。谢谢你。