我希望程序在捕获异常时重新执行while循环 - 异常是接收数字零。相反,它继续使用下面的代码循环,我希望它再次询问用户输入,直到用户输入一个不同的数字为止。
import java.util.InputMismatchException;
import java.util.Scanner;
public class whilePerjashtim {
public static int division(int a, int b){
return a/b;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b;
System.out.println("Enter a value: ");
a = s.nextInt();
while(true){
try
{
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of division is: " + division(a,b));
}
catch(ArithmeticException e)
{
System.err.println("Don't divide by zero!!!");
}
catch (java.util.InputMismatchException e)
{
System.err.println("Enter just a Number!!!");
}
finally
{
System.out.println();
}
}
}
}
答案 0 :(得分:1)
使用以下形式的内容(对于您的作业问题不是精确的Java)
boolean validInput = false;
while (!validInput) {
.. get input
.. set validInput = true if no error
.. catch error
.. print try again message
}
答案 1 :(得分:0)
您可以设置一个布尔值,用于确定while
循环是否成功结束。然后在每个循环中,首先假设值为true
,并且当引发异常时,将其设置为false
。
boolean success = false;
while(success == false){
success = true;
try {
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of divison is: " + division(a,b));
}
catch(ArithmeticException e) {
System.err.println("Dont divide by zero!!!");
success = false;
}
}
答案 2 :(得分:0)
在while循环之外定义一个布尔值,并将其用于while条件。
假设我正确地理解了你的问题,你想要留在循环中,如果用户的输入引发异常,即它是无效输入,并且你想要从用户获得有效输入时突破循环。 / p>
boolean gotValidInput = false;
while (!gotValidInput) {
try {
System.out.println("Enter b value");
b = s.nextInt();
gotValidInput = true;
System.out.println("Sum of divison is: " + division(a,b));
} catch(ArithmeticException e) {
System.err.println("Dont divide by zero!!!");
} catch (java.util.InputMismatchException e) {
System.err.println("Enter just a Number!!!");
} finally {
System.out.println();
}
}
在这个实现中,在gotValidInput = true;
被评估之前会抛出两个异常,所以如果没有抛出异常,它只会被设置为true。
答案 3 :(得分:0)
您可以添加额外的外部循环,例如
while (true) {
System.out.println("Enter a value: ");
a = s.nextInt();
while(true) {
/// terminate the loop in case of problem with a, and allow a user to re done
}
}
答案 4 :(得分:0)
清除警告并将s
移至main
方法之外,并将其定义为static
。如果在主要内部并且从未关闭,则看起来s是资源泄漏。
import java.util.InputMismatchException;
import java.util.Scanner;
public class whilePerjashtim {
private static Scanner s;
public static int division(int a, int b) {
return a / b;
}
public static void main(String[] args) {
int a, b;
s = new Scanner(System.in);
System.out.println("Enter a value: ");
a = s.nextInt();
boolean input = false;
while (input == false) {
try {
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of division is: " + division(a, b));
input = true;
}
catch (ArithmeticException e) {
System.err.println("Don't divide by zero!!!");
}
catch (InputMismatchException e) {
System.err.println("Enter just a Number!!!");
}
finally {
System.out.println();
}
}
}
}
答案 5 :(得分:0)
如果你想让while循环正常继续,你还需要处理错误的输入:你需要在每个catch块的末尾摆脱错误的输入。添加continue将简单地使循环再次运行,直到用户提供正确的输入。
catch (InputMismatchException e)
{
System.err.println("Enter just a Number!!!");
//add this
s.nextLine();
continue;
}