提问人:Vincent 提问时间:8/4/2023 最后编辑:Vincent 更新时间:8/4/2023 访问量:31
每次在扫描仪中键入人员姓名时,我总是出现 InputMismatchException 错误?
I keep having InputMismatchException error every time I type the name of the person in the Scanner?
问:
我的代码有什么问题,为什么每次在控制台中键入名称时都出现 InputMismatchException 错误?
Scanner sc = new Scanner(System.in);
System.out.println("How many People are there ? (employee and students included)");
int numPeople = sc.nextInt();
Person[] personArray = new Person[numPeople];
Student[] studentsArray = new Student[numPeople];
Employee[] employeesArray = new Employee[numPeople];
int i=0;
while(i < numPeople) {
System.out.println("Please enter the name and age of the person");
String name = sc.nextLine();
int age = sc.nextInt();sc.nextLine();
Person person = new Person(name, age);
personArray[i] = person;
i++;
答: 暂无答案
评论
int age = sc.nextInt();sc.nextLine();
String name = sc.nextLine();
String name = sc.next();