因此,每次我运行此问题时,它都会编译并运行,直到我输入第一个名称:
代码:
import java.util.Scanner;
public class BestStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input= new Scanner(System.in);
System.out.println("Enter the number of students:");
int numStudents= input.nextInt();
String highName=" ";
double highScore=0.0;
int i;
for(i=1;i<numStudents+1;i++){
System.out.println("Student "+i+" of "+numStudents);
System.out.println("Enter the student's name:");
String name=input.nextLine();
System.out.println("Enter students score:");
double score=input.nextDouble();
if(score>highScore) {
highScore=score;
highName=name;
}
}
System.out.println("The highest score was "+highScore+" and "+highName+" got it");
}
}
这是我得到的错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at BestStudent.main(BestStudent.java:22)
我已经查看了有关此内容的其他主题,并且我已经仔细检查了所有变量是否已正确初始化。