Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 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!!!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ If strikes occur, are they legally regulated?

Answered: 1 week ago

Question

=+industrial action Under what circumstances can unions strike?

Answered: 1 week ago

Question

=+What forms of industrial action are common?

Answered: 1 week ago