Ask Question
16 March, 03:27

Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from - 20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message:

+5
Answers (1)
  1. 16 March, 04:17
    0
    import java. util. Scanner;

    public class DailyTemps

    {

    public static void main (String[] args) {

    int total = 0, count = 0;

    double avg = 0;

    Scanner input = new Scanner (System. in);

    System. out. print ("Enter a temprature: ");

    double temperature = input. nextDouble ();

    while (temperature! = 999) {

    if (temperature > = - 20 && temperature < = 130) {

    total + = temperature;

    count + +;

    }

    else

    System. out. println ("Invalid Input!");

    System. out. print ("Enter a temprature: ");

    temperature = input. nextDouble ();

    }

    avg = total / (double) count;

    System. out. printf ("The average of " + count + " temperature is: %.2f", avg);

    }

    }

    Explanation:

    - Initialize the variables

    - Ask the user for the temperature

    - Initialize the while loop that iterates until the user enters 999

    - Check temperature if it is between - 20 and 130, if yes, add it to the total and increment the count by 1. If it is not between the interval, print an error message

    - Keep asking the temperature

    - Calculate and print the average of the temperatures
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers