File tree Expand file tree Collapse file tree 2 files changed +53
-1
lines changed
src/org/launchcode/java/demos/collections Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 1+ package org .launchcode .java .demos .collections ;
2+
3+ import java .util .Scanner ;
4+
5+ public class ArrayGradebook {
6+
7+ public static void main (String [] args ) {
8+
9+ // Allow for at most 30 students
10+ int maxStudents = 30 ;
11+
12+ String [] students = new String [maxStudents ];
13+ double [] grades = new double [maxStudents ];
14+ Scanner in = new Scanner (System .in );
15+
16+ String newStudent ;
17+ int numStudents = 0 ;
18+
19+ System .out .println ("Enter your students (or ENTER to finish):" );
20+
21+ // Get student names
22+ do {
23+ newStudent = in .nextLine ();
24+
25+ if (!newStudent .equals ("" )) {
26+ students [numStudents ] = newStudent ;
27+ numStudents ++;
28+ }
29+
30+ } while (!newStudent .equals ("" ));
31+
32+ // Get student grades
33+ for (int i = 0 ; i < numStudents ; i ++) {
34+ System .out .print ("Grade for " + students [i ] + ": " );
35+ double grade = in .nextDouble ();
36+ grades [i ] = grade ;
37+ }
38+
39+ // Print class roster
40+ System .out .println ("\n Class roster:" );
41+ double sum = 0.0 ;
42+
43+ for (int i = 0 ; i < numStudents ; i ++) {
44+ System .out .println (students [i ] + " (" + grades [i ] + ")" );
45+ sum += grades [i ];
46+ }
47+
48+ double avg = sum / numStudents ;
49+ System .out .println ("Average grade: " + avg );
50+ }
51+
52+ }
Original file line number Diff line number Diff line change 33import java .util .ArrayList ;
44import java .util .Scanner ;
55
6- public class Gradebook {
6+ public class ArrayListGradebook {
77
88 public static void main (String [] args ) {
99
You can’t perform that action at this time.
0 commit comments