Question #114400

Write the complete java program called Temperature that includes a for loop structure,

prompting the user to enter 5 temperature values. The program should add up each

temperature entered within the loop and this is stored in a variable called total. Using a

system statement, output the total temperature value and the average temperature value.

Expert's answer

import java.util.Scanner;

public class Temperature {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double total = 0;
        for (int i = 0; i < 5; i++)
        {
            System.out.println("Enter temperature: ");
            double temperature = scanner.nextDouble();
            total += temperature;

        }
        System.out.println("The total temperature = " + total);
        System.out.println("The average temperature = " + total / 5);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS