Finish writing the program that will ask the user to enter a number of temperatu
ID: 3776957 • Letter: F
Question
Finish writing the program that will ask the user to enter a number of temperatures and then will calculate the average temperature. First ask the user to enter in the total temperatures to be recorded. Then ask the user to enter each temperature. Each temperature should be stored in an array called temps. Print the average of all the temperatures. Test the program with two sets of data. import java.util.Scanner; public class Exercise {public static void main (String [] args){Scanner kb = new Scanner(System.in); System.out.print("Please type the number of temperatures to enter: "); int num = kb.nextlnt();Explanation / Answer
Exercise3.java
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Please type the number of temperatures to enter: ");
int num = kb.nextInt();
double temps[] = new double[num];
for(int i=0; i<num; i++){
System.out.print("Enter the temperature #"+(i+1)+": ");
temps[i] = kb.nextDouble();
}
double sum = 0;
for(int i=0; i<num; i++){
sum = sum + temps[i];
}
double average = sum/num;
System.out.println("Average temperature is "+average);
}
}
Output:
Please type the number of temperatures to enter: 5
Enter the temperature #1: 1.1
Enter the temperature #2: 2.2
Enter the temperature #3: 3.3
Enter the temperature #4: 4.4
Enter the temperature #5: 5.5
Average temperature is 3.3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.