-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAverage1.java
More file actions
57 lines (44 loc) · 1.8 KB
/
TestAverage1.java
File metadata and controls
57 lines (44 loc) · 1.8 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
54
55
56
57
//TestAverage1.java
import java.util.Scanner; // Needed for the scanner class
/**
This program demonstrates a user controlled loop.
*/
public class TestAverage1
{
public static void main(String[] args)
{
int score1, score2, score3; // Three test scores
double average; // Average test score
char repeat; // To hold 'y' or 'n'
String input; // To hold input
System.out.println("This program calculates the " +
"average of three test scores.");
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get as many sets of test scores as the user wants.
do
{
// Get the first test score in this set.
System.out.print("Enter score #1: ");
score1 = keyboard.nextInt();
// Get the second test score in this set.
System.out.print("Enter score #2: ");
score2 = keyboard.nextInt();
// Get the third test score in this set.
System.out.print("Enter score #3: ");
score3 = keyboard.nextInt();
// Consume the remaining newline.
keyboard.nextLine();
// Calculate and print the average test score.
average = (score1 + score2 + score3) / 3.0;
System.out.println("The average is " + average);
System.out.println(); // Prints a blank line.
// Does the user want to average another set?
System.out.println("Would you like to average " +
"another set of test scores?");
System.out.println("Enter Y for yes or N for no: ");
input = keyboard.nextLine(); // Read a line.
repeat = input.charAt(0); // Get the first char.
} while (repeat == 'Y' || repeat == 'y');
}
}