这段简单的代码:
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args){
Scanner sc1 = new Scanner(System.in);
int number1 = sc1.nextInt();
sc1.close();
Scanner sc2 = new Scanner(System.in);
int number2 = sc2.nextInt();
sc2.close();
}
}
总是给我这个错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at mainpkg.TestScanner.main(TestScanner.java:14)
这个也不行。这一次它陷入了一个永无止境的循环!
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args){
Scanner sc1 = new Scanner(System.in);
int number1 = sc1.nextInt();
sc1.close();
Scanner sc2 = new Scanner(System.in);
while(!sc2.hasNextInt())
;
int number2 = sc2.nextInt();
sc2.close();
}
}
为什么?