Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im writing a program that allows the user to input integers in the range from 0

ID: 3712304 • Letter: I

Question

Im writing a program that allows the user to input integers in the range from 0 to 100, until the sentinel value 999 is entered to end the program. Im trying to write a loop that will display a message if the user inputs an integer outside the range from 0 to 100.

This is the message I want to display:  

Enter only 0....100, 999 to quit

This is the code I have so far:

import java.util scanner

Public class TestAverage

{

public static void main(String[]args)

{

int score;

int count;

final int sentinel=999;

Scanner kbd= new Scanner(System.in);

System.out.print("Enter test scores, and get lowest score, the highest score, and the average"/n"Enter 999 to quit");

String prompt ="Enter a score [0....100] 999 to quit"

boolean goodScore=(score>= 0 && score <=100);

while (score != sentinel)

{

system.outprint(prompt);

score = kbd.NextInt();

if(goodScore)

{

total+=score;

count++

}

else

{

System.out.print(" Enter only 0....100, 999 to quit");

}

I wrote a boolean message that allows the program to check if the input is within the range. I'm just confused as to where in the loop this message should go.

Prompt feedback would be appreciated. Thanks in advance!!!!!!

Explanation / Answer

Here is the corrected code. Also completed the remaining functionalities to find the smallest and highest score entered. Comments are included wherever code is modified, go through it and get a clear idea on how things work. Thanks.

// TestAverage.java

import java.util.Scanner;

public class TestAverage {

                public static void main(String[] args) {

                                /**

                                * required variables

                                */

                                int total = 0;

                                int count = 0;

                                int input = 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: ";

                                /**

                                * loops until input value is equal to sentinel value

                                */

                                while (input != sentinel) {

                                                System.out.print(prompt);

                                                // getting number

                                                input = kbd.nextInt();

                                                /**

                                                * Checking if the number is in range

                                                */

                                                if (input >= 0 && input <= 100) {

                                                                /**

                                                                * Valid score

                                                                */

                                                                total += input;

                                                                /**

                                                                * checking if this is the first input, if yes, setting this

                                                                * value as minimum and maximum

                                                                */

                                                                if (count == 0) {

                                                                                min = input;

                                                                                max = input;

                                                                } else {

                                                                                /**

                                                                                * not first input, checking if the current value is greater

                                                                                * than highest value, if yes making it as the highest score

                                                                                */

                                                                                if (input > max) {

                                                                                                max = input;

                                                                                }

                                                                                /**

                                                                                * checking if the current value is smaller than smallest

                                                                                * value, if yes making it as the smallest score

                                                                                */

                                                                                if (input < min) {

                                                                                                min = input;

                                                                                }

                                                                }

                                                                count++;

                                                } else if (input != sentinel) {

                                                                System.out.println(" Enter only 0....100, 999 to quit");

                                                }

                                }

                                if (count == 0) {

                                                /**

                                                * no values entered, other than 999

                                                */

                                                System.out.println("You didn't enter any scores...");

                                } else {

                                                /**

                                                * finding average, and displaying all stats

                                                */

                                                double average = (double) total / count;

                                                System.out.println("Lowest score: " + min);

                                                System.out.println("Highest score: " + max);

                                                System.out.println("Average score: " + average);

                                }

                }

}

/*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: 25

Enter a score [0....100] 999 to quit: 90

Enter a score [0....100] 999 to quit: 100

Enter a score [0....100] 999 to quit: 0

Enter a score [0....100] 999 to quit: -4

Enter only 0....100, 999 to quit

Enter a score [0....100] 999 to quit: 101

Enter only 0....100, 999 to quit

Enter a score [0....100] 999 to quit: 70

Enter a score [0....100] 999 to quit: 999

Lowest score: 0

Highest score: 100

Average score: 57.0