I have a program which allows the user to enter a number for the amount of students they wish to assign a grade to. Afterwards, it prints out which student that is out of the total (for example 1 of 6) then prompts the user for their name as well as score.
The scores I have working correctly but when the final message is displayed I can't get the max name and second max name to display anything.
Enter the number of students:2
Student 1 of 2
Enter Student's name:Noah
Enter Student's score:100
Student 2 of 2
Enter Student's name:Ryan
Enter Student's score:50
The highest score was 100.0 and got it
The second highest score was 50.0 and got it
Could someone please tell me why this is happening?
public class TwoHighestScores {
public static void main(String[] args) {
//Declare variables
// Initialize Scanner
Scanner stdin = new Scanner(System.in);
// Enter the number of students
System.out.print("Enter the number of students:");
int n = stdin.nextInt();
double MaxValue = 0.0;
double secondMax = 0.0;
String MaxName = " ";
String secondMaxName = " ";
int count = 1;
for (int i = 0; i < n; i++) {
System.out.println("Student " + count + " of " + n);
System.out.print("Enter Student's name:");
String name = stdin.nextLine();
stdin.nextLine();
System.out.print(name);
count++;
System.out.print("Enter Student's score:");
double score = stdin.nextDouble();
// check
if (score > MaxValue) {
secondMax = MaxValue;
MaxName = name;
secondMaxName = MaxName;
MaxValue = score;
} else if (score > secondMax) {
secondMax = score;
secondMaxName = name;
}
}
System.out.println("The highest score was " + MaxValue + " and " + MaxName + " got it");
System.out.println("The second highest score was " + secondMax + " and " + secondMaxName + " got it");
}
}