java这个为什么只要在a方法里面一输入1调用b方法回来之后nextLine就会报错,不调用b就没问题

public class test {
public static void main(String[] args) throws IOException {
new test().a();
}

public void a() {
    Scanner  input = new Scanner(System.in);
    String choice = input.nextLine();
    while(!choice.equals("")) {
        System.out.println(choice);
        if(choice.equals("1")) {
            b();
        }
        choice = input.nextLine();
    }
    input.close();
}

public void b() {
    Scanner  s = new Scanner(System.in);
    System.out.println(s.nextLine());
    s.close();
}

}

因为a方法中还没close

多个scanner对象的情况下,关闭了其中一个就会导致System.in也关闭,自然就无法继续读了。

b方法中关闭了输入流

b中的流不应该关闭。因为你a中没有关闭