我不知道如何在 JAVA 中正确使用 "try"
I can't figure how to use correctly "try" in JAVA
我必须处理 "application" 上的错误 "InputMismatchException",它根据用户输入的运算符计算数字。
我在我的鳕鱼中使用 "try" 但之后我的操作 "gather, reduction, division, multiplication" 不再起作用。
如果用户输入一个数字,例如当他必须输入一个运算符并处理错误时,我应该在我的代码中的什么地方使用这个 "try",而不是让我的构建失败并继续询问用户他想做什么。
package t3;
import java.util.InputMismatchException;
import java.util.Scanner;
public class T3 {
public static void main(String[] args) {
String answer;
do{
Scanner ab = new Scanner(System.in);
System.out.println("Input what operation do you want to do!");
System.out.println("For gather: \"+\"");
System.out.println("For reduction: \"-\"");
System.out.println("For division: \"/\"");
System.out.println("For multiplication: \"*\"");
System.out.print(":");
String operation = ab.nextLine();
try{
System.out.print("Input first number: ");
int nr1 = ab.nextInt();
System.out.print("Input second number: ");
int nr2 = ab.nextInt();
int gather, reduction, division, multiplication;
gather=nr1+nr2;
reduction=nr1-nr2;
division=nr1/nr2;
multiplication=nr1*nr2;
}
catch (InputMismatchException e)
{ System.out.println(" ------------------------------ You have to choose an operation ------------------------------"); }
switch (operation){
case "+" : System.out.println("The result is: " +gather);
break;
case "-" : System.out.println("The result is: " +reduction);
break;
case "/" : System.out.println("The result is: " +division);
break;
case "*" : System.out.println("The result is: " +multiplication);
break;
default : System.out.println(" !!!! You haven't chosen an operation... \n **** Input smth like: \n+ -> gather\n- -> reduction\n/ -> division\n* -> multiplication");
}
System.out.println("Do you keep going? Yes/ No");
Scanner tx = new Scanner(System.in);
answer= tx.nextLine();
}
while(answer.equalsIgnoreCase("Yes"));
}
}
编辑:在我使用 "try" 之后,如果我输入正常输入,它会给我这个错误:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at tema2.Tema2.main(Tema2.java:104)
C:\Users\VALENTIN-CLAUDIUCOST\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\VALENTIN-CLAUDIUCOST\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 2 seconds)
import java.util.InputMismatchException;
import java.util.Scanner;
public class T3 {
public static void main(String[] args) {
String answer;
do {
Scanner ab = new Scanner(System.in);
System.out.println("Input what operation do you want to do!");
System.out.println("For gather: \"+\"");
System.out.println("For reduction: \"-\"");
System.out.println("For division: \"/\"");
System.out.println("For multiplication: \"*\"");
System.out.print(":");
String operation = ab.nextLine();
try {
System.out.print("Input first number: ");
int nr1 = ab.nextInt();
System.out.print("Input second number: ");
int nr2 = ab.nextInt();
int gather, reduction, division, multiplication;
gather = nr1 + nr2;
reduction = nr1 - nr2;
division = nr1 / nr2;
multiplication = nr1 * nr2;
switch (operation) {
case "+":
System.out.println("The result is: " + gather);
break;
case "-":
System.out.println("The result is: " + reduction);
break;
case "/":
System.out.println("The result is: " + division);
break;
case "*":
System.out.println("The result is: " + multiplication);
break;
default:
System.out.println(" !!!! You haven't chosen an operation... \n **** Input smth like: \n+ -> gather\n- -> reduction\n/ -> division\n* -> multiplication");
}
} catch (InputMismatchException e) {
System.out.println(" ------------------------------ You have to choose an operation ------------------------------");
}
System.out.println("Do you keep going? Yes/ No");
Scanner tx = new Scanner(System.in);
answer = tx.nextLine();
}
while (answer.equalsIgnoreCase("Yes"));
}
}
修复了代码格式问题,因为它需要访问在其中声明的变量,所以需要在 try 块中移位 switch
。
我必须处理 "application" 上的错误 "InputMismatchException",它根据用户输入的运算符计算数字。 我在我的鳕鱼中使用 "try" 但之后我的操作 "gather, reduction, division, multiplication" 不再起作用。 如果用户输入一个数字,例如当他必须输入一个运算符并处理错误时,我应该在我的代码中的什么地方使用这个 "try",而不是让我的构建失败并继续询问用户他想做什么。
package t3;
import java.util.InputMismatchException;
import java.util.Scanner;
public class T3 {
public static void main(String[] args) {
String answer;
do{
Scanner ab = new Scanner(System.in);
System.out.println("Input what operation do you want to do!");
System.out.println("For gather: \"+\"");
System.out.println("For reduction: \"-\"");
System.out.println("For division: \"/\"");
System.out.println("For multiplication: \"*\"");
System.out.print(":");
String operation = ab.nextLine();
try{
System.out.print("Input first number: ");
int nr1 = ab.nextInt();
System.out.print("Input second number: ");
int nr2 = ab.nextInt();
int gather, reduction, division, multiplication;
gather=nr1+nr2;
reduction=nr1-nr2;
division=nr1/nr2;
multiplication=nr1*nr2;
}
catch (InputMismatchException e)
{ System.out.println(" ------------------------------ You have to choose an operation ------------------------------"); }
switch (operation){
case "+" : System.out.println("The result is: " +gather);
break;
case "-" : System.out.println("The result is: " +reduction);
break;
case "/" : System.out.println("The result is: " +division);
break;
case "*" : System.out.println("The result is: " +multiplication);
break;
default : System.out.println(" !!!! You haven't chosen an operation... \n **** Input smth like: \n+ -> gather\n- -> reduction\n/ -> division\n* -> multiplication");
}
System.out.println("Do you keep going? Yes/ No");
Scanner tx = new Scanner(System.in);
answer= tx.nextLine();
}
while(answer.equalsIgnoreCase("Yes"));
}
}
编辑:在我使用 "try" 之后,如果我输入正常输入,它会给我这个错误:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at tema2.Tema2.main(Tema2.java:104)
C:\Users\VALENTIN-CLAUDIUCOST\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\VALENTIN-CLAUDIUCOST\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 2 seconds)
import java.util.InputMismatchException;
import java.util.Scanner;
public class T3 {
public static void main(String[] args) {
String answer;
do {
Scanner ab = new Scanner(System.in);
System.out.println("Input what operation do you want to do!");
System.out.println("For gather: \"+\"");
System.out.println("For reduction: \"-\"");
System.out.println("For division: \"/\"");
System.out.println("For multiplication: \"*\"");
System.out.print(":");
String operation = ab.nextLine();
try {
System.out.print("Input first number: ");
int nr1 = ab.nextInt();
System.out.print("Input second number: ");
int nr2 = ab.nextInt();
int gather, reduction, division, multiplication;
gather = nr1 + nr2;
reduction = nr1 - nr2;
division = nr1 / nr2;
multiplication = nr1 * nr2;
switch (operation) {
case "+":
System.out.println("The result is: " + gather);
break;
case "-":
System.out.println("The result is: " + reduction);
break;
case "/":
System.out.println("The result is: " + division);
break;
case "*":
System.out.println("The result is: " + multiplication);
break;
default:
System.out.println(" !!!! You haven't chosen an operation... \n **** Input smth like: \n+ -> gather\n- -> reduction\n/ -> division\n* -> multiplication");
}
} catch (InputMismatchException e) {
System.out.println(" ------------------------------ You have to choose an operation ------------------------------");
}
System.out.println("Do you keep going? Yes/ No");
Scanner tx = new Scanner(System.in);
answer = tx.nextLine();
}
while (answer.equalsIgnoreCase("Yes"));
}
}
修复了代码格式问题,因为它需要访问在其中声明的变量,所以需要在 try 块中移位 switch
。