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