如何在此代码中捕获双精度 a、b 和 c 的 InputMismatchException?
How do I catch InputMismatchException for doubles a, b, and c in this code?
所以我制作了一个求解二次公式的计算器,但我希望程序在变量 a、b 或 c 不是有效的双精度数时抛出异常。但是,我不知道如何将变量放入我希望它们所在的方程式中,所以这里是代码。
我不知道要介绍什么背景,我对 java 编程真的很陌生,我在其他任何地方都找不到我的具体问题的答案。
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //scanner
short repeat = 1;
while (repeat == 1) {
System.out.println("Enter your equation by entering a, b, and c."); //introduction
System.out.println("Press enter evey time you enter a number.");
try {
double a = input.nextDouble();
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
}
double b = input.nextDouble();
double c = input.nextDouble();
double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);
System.out.println("Your answers are: " + answer1 + " and " + answer2);
System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
repeat = input.nextShort();
}
input.close();
}
我想让 try/catch 工作并输出一个可以在方程中使用的值,但是方程没有注册变量 a。我将如何着手执行 try/catch 或任何其他显示错误消息的方法?
根据 Java Docs Scanner
抛出 InputMismatchException
如果下一个值不是有效的双精度值,因此您不应该手动处理它。
但是如果你想在异常发生时显示消息,你应该用try-catch块捕获它并做你想做的。
the equations don't register the variable a
发生这种情况是因为 a
是 "try" 块作用域的一部分。一旦你退出这个块,变量就消失了。
您可能想要做的是:
double a; //declaring variable a (main() method scope)
try {
a = input.nextDouble(); //assigning a new value
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
throw e; //notice, the exception is still thrown after the message is printed
}
现在 a
在 main
方法的范围内,您可以在 try-catch 块之后使用它。
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //scanner
short repeat = 1;
while (repeat == 1) {
System.out.println("Enter your equation by entering a, b, and c."); //introduction
System.out.println("Press enter evey time you enter a number.");
try {
double a = Double.parseDouble(input.nextLine());
double b = Double.parseDouble(input.nextLine());
double c = Double.parseDouble(input.nextLine());
double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);
System.out.println("Your answers are: " + answer1 + " and " + answer2);
}
catch (NumberFormatException e) {
System.out.println("That's not a valid number.");
}
System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
repeat = Short.parseShort(input.nextLine());
}
input.close();
}
您已将 repeat 变量初始化为 1 而不是 0,否则程序将不会进入 while 循环。
您也需要将 answer1 和 answer2 的计算放在 try-catch-block 中,因为如果用户输入了无效值,则不应计算结果。
你应该只使用一个 Scanner 实例,所以你不能在循环中实例化它。
所以我制作了一个求解二次公式的计算器,但我希望程序在变量 a、b 或 c 不是有效的双精度数时抛出异常。但是,我不知道如何将变量放入我希望它们所在的方程式中,所以这里是代码。
我不知道要介绍什么背景,我对 java 编程真的很陌生,我在其他任何地方都找不到我的具体问题的答案。
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //scanner
short repeat = 1;
while (repeat == 1) {
System.out.println("Enter your equation by entering a, b, and c."); //introduction
System.out.println("Press enter evey time you enter a number.");
try {
double a = input.nextDouble();
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
}
double b = input.nextDouble();
double c = input.nextDouble();
double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);
System.out.println("Your answers are: " + answer1 + " and " + answer2);
System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
repeat = input.nextShort();
}
input.close();
}
我想让 try/catch 工作并输出一个可以在方程中使用的值,但是方程没有注册变量 a。我将如何着手执行 try/catch 或任何其他显示错误消息的方法?
根据 Java Docs Scanner
抛出 InputMismatchException
如果下一个值不是有效的双精度值,因此您不应该手动处理它。
但是如果你想在异常发生时显示消息,你应该用try-catch块捕获它并做你想做的。
the equations don't register the variable a
发生这种情况是因为 a
是 "try" 块作用域的一部分。一旦你退出这个块,变量就消失了。
您可能想要做的是:
double a; //declaring variable a (main() method scope)
try {
a = input.nextDouble(); //assigning a new value
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
throw e; //notice, the exception is still thrown after the message is printed
}
现在 a
在 main
方法的范围内,您可以在 try-catch 块之后使用它。
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //scanner
short repeat = 1;
while (repeat == 1) {
System.out.println("Enter your equation by entering a, b, and c."); //introduction
System.out.println("Press enter evey time you enter a number.");
try {
double a = Double.parseDouble(input.nextLine());
double b = Double.parseDouble(input.nextLine());
double c = Double.parseDouble(input.nextLine());
double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);
System.out.println("Your answers are: " + answer1 + " and " + answer2);
}
catch (NumberFormatException e) {
System.out.println("That's not a valid number.");
}
System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
repeat = Short.parseShort(input.nextLine());
}
input.close();
}
您已将 repeat 变量初始化为 1 而不是 0,否则程序将不会进入 while 循环。
您也需要将 answer1 和 answer2 的计算放在 try-catch-block 中,因为如果用户输入了无效值,则不应计算结果。
你应该只使用一个 Scanner 实例,所以你不能在循环中实例化它。