-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise05_09.java
62 lines (47 loc) · 1.85 KB
/
Exercise05_09.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ch_05;
import java.util.Scanner;
/**
* 5.9 (Find the two highest scores) Write a program that prompts
* the user to enter the number of students and each student�s name
* and score, and finally displays the student with the highest score
* and the student with the second-highest score. Use the next() method
* in the Scanner class to read a name rather using the nextLine() method.
*/
public class Exercise05_09 {
public static void main(String[] args) {
double highScore = 0, secondHigh = 0;
String highName = "";
String secondName = "";
String tempName = "";
double tempScore = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numStudents = input.nextInt();
System.out.print("Enter a students name: ");
highName = input.next();
System.out.print("Enter the students score: ");
highScore = input.nextDouble();
while (numStudents > 1) {
System.out.print("Enter a students name: ");
tempName = input.next();
System.out.print("Enter the students score: ");
tempScore = input.nextDouble();
if (tempScore > highScore) {
secondHigh = highScore;
secondName = highName;
highScore = tempScore;
highName = tempName;
numStudents--;
continue;
}
if (tempScore < highScore && tempScore > secondHigh) {
secondHigh = tempScore;
secondName = tempName;
}
numStudents--;
}
System.out.println("The top two students are: ");
System.out.print(highName + "'s score of " + highScore);
System.out.print(" and " + secondName + "'s score of " + secondHigh);
}
}