Ask Question
27 March, 04:22

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. 27 March, 06:07
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers