forked from LaunchCodeEducation/java-web-dev-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapPractice.java
More file actions
46 lines (35 loc) · 1.15 KB
/
HashMapPractice.java
File metadata and controls
46 lines (35 loc) · 1.15 KB
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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HashMapPractice {
public static void main(String[] args) {
HashMap<String, Double> students = new HashMap<>();
Scanner input = new Scanner(System.in);
String newStudent;
System.out.println("Enter your students (or ENTER to finish):");
// Get student names and grades
do
{
System.out.print("Student: ");
newStudent = input.nextLine();
if (!newStudent.isEmpty()) {
System.out.print("Id: ");
Double newId = input.nextDouble();
students.put(newStudent, newId);
// Read in the newline before looping back
input.nextLine();
}
} while(!newStudent.isEmpty());
// Print class roster
System.out.println("\nClass roster:");
double sum = 0.0;
for(
Map.Entry<String, Double> student :students.entrySet())
{
System.out.println(student.getKey() + " (" + student.getValue() + ")");
sum += student.getValue();
}
double avg = sum / students.size();
System.out.println("Average grade: "+avg);
}
}