日期:2014-05-20 浏览次数:21033 次
package ex13;
import java.util.*;
public class InputMismatchExceptionDemo {
    
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        Scanner input=new Scanner(System.in);
        boolean continueInput=true;
        
        do{
            try{
                System.out.print("Enter an integer: ");
                int number=input.nextInt();
                
                //Display the result
                System.out.println("The number entered is "+number);
                
                continueInput=false;
            }
            catch(InputMismatchException ex){
                System.out.println("Try again.("+
                        "Incorrect input:an integer is required");
                //input.nextLine();这句注释掉就会有死循环,不得其解
            }
        }while(continueInput);
    }
}