解决方案 »

  1.   

    简单说下,不知道楼主有没有学过异常处理,由于你提到的这种输入类型不匹配是属于非检查常,所以编码时是不加try..catch是不会报错的,但是运行时,一旦出现异常没有捕获的话该异常就会直接抛给虚拟机,而虚拟机的处理方法是会直接终止下面编码的执行并printStackTrace写出出现异常的堆栈信息,如果你还想继续执行后面编写的编码就必须自己捕获异常。比如Scanner scan=new Scanner(System.in);
    int a=0;
    try{   a=scan.nextInt();}catch(RuntimeException e){    e.printStackTrace
    }
    ...这样你后面代码就可以执行了,不过如果出现数据类型不匹配异常(NumberFormatException)那么a就为初始值0.
      

  2.   

    不知道你想要的是不是下面的例子
    public class Test {
    public static void main(String[] args) throws Exception { while(true) {
    try {
    Scanner s = new Scanner(System.in);
    int i = Integer.valueOf(s.nextLine());
    System.err.println(i);
    } catch (Exception e) {
    System.err.println("cause: " + e.getMessage());
    }
    } }
    }
      

  3.   

    构造一个for循环,在里面你可以先将输入的数据保存在一个Object类型的变量中,在判断它是否属于Integer类型,如果不是则继续循环,会再次要求输入数据;如是Integer类型则将其赋给你的int变量。具体代码可以参考equals的重写代码,两者有些类似
      

  4.   

    直接try ...catch。   如果异常了 在调用一次就ok
      

  5.   

    你在catch中再次调用,应该就可以了
      

  6.   

    try..catch放到while(true)循环中,只要有错误就一直让他输入,没错误就break
      

  7.   

    先判断该字符串是不是int,如果不是就再等待新数据public boolean isInt(String str){
      if(str==null||"".equals(str)){
        return false;
      }
      for(char the:str.toCharArray()){
        if(the<'0'||the>'9')
          return false;
      }
      return true;
    }