我试图让这个问题正常工作,它要求输入一些分数然后它应该显示得分最高的人的名字。我将输入的最后一个分数作为最高分,问题是输入的最后一个分数不一定是输入的最高分数。任何有关如何解决此问题的想法将不胜感激。这是家庭作业,所以没有人说“使用列表或数组”,我们没有在课堂上讨论过,因此不应该用它来解决这个问题。
public static void main(String[] args)
{
// variables
Scanner input = new Scanner(System.in);
int count = 0;
int numStudents;
double grade = 0, highestGrade = 0;
String name = "", highName = "";
String numGrades =
JOptionPane.showInputDialog
("How many student grades are you entering: ");
numStudents = Integer.parseInt(numGrades);
//for(int count = 0; count < numStudents; count++)
while(count < numStudents)
{
// prompt for the user to enter grades
String inputName =
JOptionPane.showInputDialog("Enter a student name: ");
name = inputName;
//name = input.next(inputName);
String inputGrade =
JOptionPane.showInputDialog("What is that students grade: ");
grade = Double.parseDouble(inputGrade);
//grade = input.nextDouble();
count++;
//if(grade < highestGrade)
if(highestGrade > grade)
{
name = highName;
grade = highestGrade;
}
else
{
continue;
}
}
JOptionPane.showMessageDialog
(null, "The student with the highest score is " + name +
" with a grade of " + grade);
}