-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAverage2.java
More file actions
53 lines (42 loc) · 1.48 KB
/
TestAverage2.java
File metadata and controls
53 lines (42 loc) · 1.48 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
47
48
49
50
51
52
53
//TestAverage2.java
import java.util.Scanner;
/**
This program demonstrates a nested loop.
*/
public class TestAverage2
{
public static void main(String[] args)
{
int numStudents, // Number of students
numTests, // Number of tests per students
score, // Test score
total; // Accumulator for test scores
double average; // Average test score
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the number of students.
System.out.print("How many students do you have? ");
numStudents = keyboard.nextInt();
// Get the number of test scores per student.
System.out.print("How many test scores per student? ");
numTests = keyboard.nextInt();
// Process all the students
for (int student = 1; student <= numStudents; student++)
{
total = 0; // Set the accumulator to zero.
// Get the test scores for a student.
System.out.println("Studnent number " + student);
System.out.println("---------------------");
for (int test = 1; test <= numTests; test++)
{
System.out.print("Enter score " + test + ": ");
score = keyboard.nextInt();
total += score; // Add score to total
}
// Calculate and display the average.
average = total / numTests;
System.out.printf("The average for student %d is %.1f.\n\n",
student, average);
}
}
}