Reputation: 199
I am trying to get this problem to work right, what it does it ask for some scores to be entered and then it is supposed to show the name of the person with the highest score. I am getting the last score entered as the highest score, the problem with that is the last score entered is not going to necessarily be the highest one entered. Any ideas on how I can fix this would be appreciated. This is homework and just so nobody says "use a list or array" we have not covered that in class and thus are not supposed to use it for this problem.
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);
}
Upvotes: 1
Views: 83
Reputation: 121387
if(highestGrade > grade)
{
name = highName;
grade = highestGrade;
}
Since you want to find the highest grade and name, the above should be in reverse.
if(grade > highestGrade )
{
highName = name;
highestGrade = grade;
}
Also, print the found values:
JOptionPane.showMessageDialog
(null, "The student with the highest score is " + highName +
" with a grade of " + highestGrade);
Upvotes: 3
Reputation: 78650
This is backwards:
if(highestGrade > grade)
should be
if(highestGrade < grade)
or more understandably:
if(grade > highestGrade)
EDIT: This is also backwards...
name = highName;
grade = highestGrade;
Upvotes: 2