-1

如果用户输入一个非数字值,例如“你好”,我想给用户一个新的机会来输入一个数字值,直到他成功。

运行下面的代码,如果用户输入非数字值,文本“这不是数字值。输入数字值:”将显示在控制台中。但是,用户将无法输入新值。因为错误消息“线程中的异常...”将显示并停止程序。

我该如何解决这个问题?

编辑!
新代码(此代码有效,它完全停止程序。之后调用的每个方法都不会运行。)Edit2!那没起效!

int number;

do {
  try {
    System.out.println("Enter a numeric value: ");
    number = Integer.parseInt(s.nextLine());

    if (s.nextInt() != (int)number) {
      throw new NumberFormatException();
    }
    break;
  }
  catch (NumberFormatException e) {
    System.out.println("This is not a numeric value. Enter a numeric value: ");
  }
} while (true)

旧代码

int number;

try {
  Scanner s = new Scanner(System.in);

  System.out.println("Enter a numeric value: ");
  number = s.nextInt();

  if (number != (int)number) {
    throw new Exception();
  }
}
catch(Exception e) {
  do {
    System.out.println("This is not a numeric value. Enter a numeric value: ");
    number = s.nextInt();
  } while (number != (int)number);
}
4

5 回答 5

2

使用 NumberFormatException 而不是基本异常类

int number;
Scanner s = new Scanner(System.in);
System.out.println("Enter a numeric value: ");

do {
    try {

        number = Integer.parseInt(s.nextLine());
        //Do your work
        break;
    } catch (NumberFormatException e) {
        System.out.println("This is not a numeric value. Enter a numeric value: ");

    }
}
while (true);

}
于 2020-07-15T17:57:51.623 回答
1

请尝试以下给定的代码:

int number;
Scanner s = new Scanner (System.in);
  System.out.println ("Enter a numeric value: ");

do
  {
try
{

  number = Integer.parseInt (s.nextLine ());
  //Do your work
  break;
}
catch (Exception e)
{
  System.out.println
    ("This is not a numeric value. Enter a numeric value: ");

}
  }
while (true);

}
于 2020-07-15T17:50:21.513 回答
1

听起来你想永远循环,直到用户输入一个有效的整数。表达“永远循环”的常用方法是使用while (true) {...}. 这是一个“无限循环”。退出此循环的唯一方法是使用break语句或return语句(或通过抛出异常,但在这种情况下您希望捕获异常并继续循环)。

使用的一个问题scanner.nextInt()是,如果该值不是整数,则该值将保留在扫描仪的缓冲区中。如果您继续调用nextInt(),您将一直得到InputMismatchExceptions ,因为扫描器会一遍又一遍地尝试将相同的错误输入解释为整数。

解决该问题的一种方法是使用scanner.nextLine()将值读取为字符串,然后将Integer.parseInt(String)其转换Stringint. 如果转换失败,则Integer.parseInt(String)抛出NumberFormatException. 您可以通过捕获它来处理此异常。

这是一个永远循环的小函数,直到用户输入一个可以解析为的值int

public static int promptForInt(Scanner scanner) {
  while (true) {
    System.out.println("Enter a numeric value:");
    try {
      return Integer.parseInt(scanner.nextLine());
    } catch (NumberFormatException e) {
      System.out.print("This is not a numeric value. ");    
    }
  }
}

您可以像这样调用该方法:

Scanner scanner = new Scanner(System.in);
int number = promptForInt(scanner);
于 2020-07-15T18:01:15.847 回答
1

我认为您并不真正了解异常的来源。该行throw new Exception(); 永远不会在您的代码中运行。这是因为你的 if 语句说if (number != (int) number),这永远不会是真的,因为变量number已经是 type int,所以你基本上是在说if (number != number),这永远不会是真的。如果你看一下 Scanner 的nextInt()方法,你会注意到它实际上抛出了InputMismatchException. 这是您使用 try-catch 捕获的异常,但不是导致错误消息的异常。那么这个异常是从哪里来的呢?该行number = s.nextInt();是不可编译的代码。您s在 try 块内定义,因此s 仅存在于try 块. 当您尝试在 catch 块中访问它时会出现异常,因为s那里没有变量;就编译器而言,它不存在。修复非常简单;只需将 for 的声明移到stry 块之外,然后将 while 循环放在 try-catch 块周围。s.nextLine()还要记住每次尝试读取 int 时都要使用输入流中的行分隔符。这是我将如何做的一个例子:

int number;
Scanner s = new Scanner(System.in); //declare s outside of the try block

System.out.println("Enter a numeric value: ");

while (true) { //while loop goes around the try-catch block
  try {
    number = s.nextInt(); //this could throw InputMismatchException
    break; //if no exception is thrown, break out of the loop
  } catch (InputMismatchException e) { //if the exception is thrown
    s.nextLine(); //consume the line separator character(s)
    System.out.println("This is not a numeric value. Enter a numeric value: "); //prompt the user for another value
    //this will then go back to the top of the try block again, because we never broke out of the while loop
  }
}

s.nextLine(); //consume the line separator character(s)
于 2020-07-15T18:33:48.573 回答
0

Try the following, if you take your input as a string you can parseInt to check if its an integer value

    String number = null;

    Scanner s = new Scanner(System.in);

    System.out.println("Enter a numeric value: ");
    number = s.next();

    try {
        // checking valid integer using parseInt() method
        Integer.parseInt(number);

    } catch (NumberFormatException e) {
        System.out.println("This is not a numeric value. Enter a numeric value:");
        number = s.next();

        // put this line here to prove it accepted the second attempt
        System.out.println("Your entered integer is:" + number);
    }
    s.close();

}

}

于 2020-07-15T18:07:42.097 回答