java try-catch inputMismatchException

时间:2016-12-17 02:44:49

标签: java

我是java try-catch的新手。任何人都可以告诉我为什么这段代码是一个无限循环?非常感谢!

import java.util.*;

public class Exercise12_02 {
  public static void main(String[] args){
    int num1 = 0, num2 = 0;
    Scanner keyboard = new Scanner(System.in);
    boolean status = false;
    while(!status){
    try{
      System.out.print("Enter the first number: ");
      num1 = keyboard.nextInt();
      System.out.print("Enter the second number: ");
      num2 = keyboard.nextInt();
      status = true;
    }catch(InputMismatchException ex){
      System.out.println("Wrong number format.");
    }
  }
    System.out.println("The sum of two numbers are "+ (num1+num2));
  }
}

0 个答案:

没有答案