Java错误处理-这是最好的方法吗?
我正在制作的控制台程序中有一个错误,我似乎无法处理错误 如果用户输入了不正确的输入类型(例如整数所在的浮点),则会对其进行谴责,并且程序会运行(即使程序关闭,也会按我的方式运行): 例如,如果用户输入一个不等于零的变量零(由于零的除法),用户将受到谴责,生活仍在继续,程序仍在运行:Java错误处理-这是最好的方法吗?,java,error-handling,Java,Error Handling,我正在制作的控制台程序中有一个错误,我似乎无法处理错误 如果用户输入了不正确的输入类型(例如整数所在的浮点),则会对其进行谴责,并且程序会运行(即使程序关闭,也会按我的方式运行): 例如,如果用户输入一个不等于零的变量零(由于零的除法),用户将受到谴责,生活仍在继续,程序仍在运行: while (b == 0) { System.out.println("Did you even read the instructions?? You cannot divide by
while (b == 0) {
System.out.println("Did you even read the instructions?? You cannot divide by zero!");
System.out.println();
System.out.println("Second Integer: ");
b = scanner.nextLong();
}
但是,如果用户试图除以零,然后输入错误的输入类型,程序将崩溃。我做错了什么?我尝试过像在其他实例中一样进入try/catch-while循环,但似乎没有达到目的:
System.out.println("Second Integer: ");
while(!scanner.hasNextLong()){
System.out.println("You entered something bad.. Why do you hurt me?");
System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
try {
Thread.sleep(4000);
} catch (InterruptedException f) {
f.printStackTrace();
}
System.exit(0);
}
b = scanner.nextLong();
while (b == 0) {
System.out.println("Did you even read the instructions?? You cannot divide by zero!");
System.out.println();
System.out.println("Second Integer: ");
b = scanner.nextLong();
}
如果您尝试执行零除法,则会抛出一个
java.lang.arithmetricException
,当您尝试读取一个long,并且用户输入了一些无法解析的内容时,会抛出InputMismatchException
(可能称为“崩溃”)
发生错误,因为您正在使用b=scanner.nextLong()代码>当用户输入0而不检查用户是否输入了有效的长值时
以下是您的代码:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long b = 0;
do {
System.out.println("Second Integer: ");
b = readLong(scanner);
if (b == 0) {
System.out.println("Did you even read the instructions?? You cannot divide by zero!");
System.out.println();
}
} while (b == 0);
}
private static long readLong(Scanner scanner) {
if (!scanner.hasNextLong()) {
System.out.println("You entered something bad.. Why do you hurt me?");
System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
try {
Thread.sleep(4000);
} catch (InterruptedException f) {
f.printStackTrace();
}
System.exit(0);
}
return scanner.nextLong();
}
您的问题是您的输入验证似乎是连续的。也就是说,检查无效输入,然后检查零值。您可能希望在每次有输入时都执行这两项操作
您可能希望在单个循环中检查所有错误情况。比如:
System.out.println("Second Integer: ");
// a loop wrapping your other validation checks
while(scanner.hasNext()){ // while there is input:
while(!scanner.hasNextLong()){ // check for non-long input
System.out.println("You entered something bad.. Why do you hurt me?");
System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
try {
Thread.sleep(4000);
} catch (InterruptedException f) {
f.printStackTrace();
}
System.exit(0);
}
b = scanner.nextLong();
while (b == 0) { // check for non-zero input
System.out.println("Did you even read the instructions?? You cannot divide by zero!");
System.out.println();
System.out.println("Second Integer: ");
b = scanner.nextLong();
}
}
“撞车”是什么意思?您是否得到异常和堆栈跟踪?正确,InputMismatchException被抛出,不是崩溃。抱歉术语上的错误,我有点糊涂。谢谢你的帮助!没问题。我们是来帮忙的。别忘了对答案投赞成票或反对票。
System.out.println("Second Integer: ");
// a loop wrapping your other validation checks
while(scanner.hasNext()){ // while there is input:
while(!scanner.hasNextLong()){ // check for non-long input
System.out.println("You entered something bad.. Why do you hurt me?");
System.out.println("*Calcumatastic dies, and I hope you feel remorseful*");
try {
Thread.sleep(4000);
} catch (InterruptedException f) {
f.printStackTrace();
}
System.exit(0);
}
b = scanner.nextLong();
while (b == 0) { // check for non-zero input
System.out.println("Did you even read the instructions?? You cannot divide by zero!");
System.out.println();
System.out.println("Second Integer: ");
b = scanner.nextLong();
}
}