为什么在这段代码中会抛出java.util.InputMismatchException?

为什么在这段代码中会抛出java.util.InputMismatchException?,java,Java,这是我的密码: public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); int m = in.nextInt(); String s = in.next

这是我的密码:

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int m = in.nextInt();
            String s = in.nextLine();
            for (int i = 0; i < m; i++) {
                int pos = in.nextInt();    // <- Line 14
                char c = (char)in.nextByte();
                s = s.substring(0, pos) + c + (pos + 1 < n ? s.substring(pos + 1) : "");
                System.out.println(f(s));
            }
        }
    }
}
我甚至没有机会输入
pos
c

有谁能告诉我这段话有什么不对吗?提前谢谢

int pos = in.nextInt();
对于无法转换为整数的b的输入。这就是引发异常的原因。

这是因为Scanner#nextInt方法不会使用输入的最后一个换行符,因此在下次调用Scanner#nextInt时会使用该换行符
int pos = in.nextInt();