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:

Respuesta :

Answer:

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

ACCESS MORE
EDU ACCESS
Universidad de Mexico