I\'m writing a program that allows the user to input integers in the range 0 to
ID: 3714249 • Letter: I
Question
I'm writing a program that allows the user to input integers in the range 0 to 100. Scores are inputted until the sentinel value 999 is entered. If a number outside the range is entered,(other than 999) the user is told the number is not allowed and is then prompted for a new one. This is what I have so far:
import java.util.Scanner;
public class TestAverage {
public static void main(String[] args) {
int total = 0;
int count = 0;
int score = 0;
int min = 0;
int max = 0;
final int sentinel = 999;
Scanner kbd = new Scanner(System.in);
System.out.println("Enter test scores, and get lowest score, the "+ "highest score, and the average Enter 999 to quit");
String prompt = "Enter a score [0....100] 999 to quit: ";
while (score != sentinel) {
System.out.print(prompt);
score= kbd.nextInt();
if(score >0 && score < 100){
total+=score;
if(count==0){
min=score;
max=score;
}
else{
if(score>max){
max=score;
}
if(score<min){
min=score;
}
}
count++;
}
else if(score != sentinel && score <0 && score > 100 ){
System.out.printf("%8d,Enter only 0..100 (999 quits)");
}
}
if(count == 0){
System.out.println("You didn't enter any scores...");
}
else{
double average = (double) total / count;
System.out.println("Lowest score: " + min);
System.out.println("Highest score: " + max);
System.out.println("Average score: " + average);
System.out.println("There were" +" " + count + " " + "scores.");
}
}
}
I'm trying to get the program to display this message when the user inputs an integer outside the range:
Enter only 0.100 (999 quits)
But anytime I input a number outside the range, it displays the standard message. Are there any tips you could give? Many thanks in advance!!!!
Explanation / Answer
TestAverage.java
import java.util.Scanner;
public class TestAverage {
public static void main(String[] args) {
int total = 0;
int count = 0;
int score = 0;
int min = 0;
int max = 0;
final int sentinel = 999;
Scanner kbd = new Scanner(System.in);
System.out.println("Enter test scores, and get lowest score, the "+ "highest score, and the average Enter 999 to quit");
String prompt = "Enter a score [0....100] 999 to quit: ";
while (score != sentinel) {
System.out.print(prompt);
score= kbd.nextInt();
if(score >=0 && score <= 100){
total+=score;
if(count==0){
min=score;
max=score;
}
else{
if(score>max){
max=score;
}
if(score<min){
min=score;
}
}
count++;
}
else if(score != sentinel ){
System.out.println("Enter only 0.100 (999 quits)");
}
}
if(count == 0){
System.out.println("You didn't enter any scores...");
}
else{
double average = (double) total / count;
System.out.println("Lowest score: " + min);
System.out.println("Highest score: " + max);
System.out.println("Average score: " + average);
System.out.println("There were" +" " + count + " " + "scores.");
}
}
}
Output:
Enter test scores, and get lowest score, the highest score, and the average
Enter 999 to quit
Enter a score [0....100] 999 to quit: 55
Enter a score [0....100] 999 to quit: 66
Enter a score [0....100] 999 to quit: 77
Enter a score [0....100] 999 to quit: -1
Enter only 0.100 (999 quits)
Enter a score [0....100] 999 to quit: 77
Enter a score [0....100] 999 to quit: 999
Lowest score: 55
Highest score: 77
Average score: 68.75
There were 4 scores.