当我输入数字以外的字符就会报错,只输入数字程序正常运行

图1是要求运行的结果

img


图2,3是我编写的程序分别输入数字,及字符的运行结果

img

img


我想知道为什么当我输入数字以外的字符会报错,以及有什么解决办法。

img


你这些地方都限定了值为int类型

什么意思哦?你指输入ID还是密码啊

因为你的用户名 ID=input.nextInt(); 这个nextInt()方法内部


```java
public int nextInt(int radix) {
        // Check cached result
        if ((typeCache != null) && (typeCache instanceof Integer)
            && this.radix == radix) {
            int val = ((Integer)typeCache).intValue();
            useTypeCache();
            return val;
        }
        setRadix(radix);
        clearCaches();
        // Search for next int
        try {
            String s = next(integerPattern());
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
                s = processIntegerToken(s);
            return Integer.parseInt(s, radix);
        } catch (NumberFormatException nfe) {
            position = matcher.start(); // don't skip bad token
          **  throw new InputMismatchException(nfe.getMessage());**
        }
    }

```

你可以使用input.next()方法取输入值,或者trycatch异常进行类型的提示

谢谢各位,我把ID的定义类型和password搞反了