public class Exercise_09 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of student: ");
int studentCount = input.nextInt();
input.nextLine();
String topSName = null;
double topSScore = 0;
String secondSName = null;
double secondSScore = 0;
for (int i = 0; i < studentCount; i++) {
System.out.print("Enter name for student #" + (i + 1) + ": ");
String s = input.next();
System.out.print("Enter score for student #" + (i + 1) + ": ");
double score = input.nextDouble();
if (score > topSScore) {
if (topSName != null) {
secondSName = topSName;
secondSScore = topSScore;
}
topSName = s;
topSScore = score;
} else if (score > secondSScore) {
secondSName = s;
secondSScore = score;
}
}
System.out.println("Top student " + topSName + "'s score is " + topSScore);
System.out.println("Second top student " + secondSName + "'s score is " + secondSScore);
}
}
Ok I got this code what it does is it ask for number of students, their names and score and displays the highest and 2nd highest score.
Now my question is that when I am repeating asking for name of the student and score how input keep the record of all the scores and names and how do i call it ? WHat the hell that if statement logic doing? i dont get that part.