我有一个带有静态方法的测试类来获取输入。
class Test {
public static Student readStudent() throws IOException {
Scanner s = new Scanner(System.in);
System.out.println("Enter first name of student");
String fname = s.nextLine();
System.out.println("Enter middle name of student");
String mname = s.nextLine();
System.out.println("Enter last name of student");
String lname = s.nextLine();
System.out.println("Enter name format(1 for ',' and 2 for ';') ");
int num = s.nextInt();
System.out.println("Enter age of student");
int age = s.nextInt();
s.close();
return new Student(new Name(String.join((num == 1) ? "," : ";", fname,
mname, lname)), age);
}
}
我能够接受一名学生的输入,但是一旦我将其放入 for 循环中,我就会收到错误java.util.NoSuchElementException: No line found。
这是我的循环
for (int i = 0; i < 10; i++) {
Student s = Test.readStudent();
}
为什么我会收到此错误?谢谢。
慕侠2389804
相关分类