Question: Create a program called AverageTemperature2YourLastName . Using a for loop, prompt the user for 7 days worth of temperatures. Find the average temperatures after all
Create a program called AverageTemperature2YourLastName. Using a for loop, prompt the user for 7 days worth of temperatures. Find the average temperatures after all the prompting is complete. Return the average temperature as a double.
Remember: no magic numbers. Good, descriptive variable names. Brief comments that describe what the code is doing.
public class AverageTemperatureBurt { private static Scanner scan;
public static void main(String[] args) { { // PROMPTING USER TO ENTER THE NUMBER OF INPUTS System.out.print("How many days of temperatures you would like to enter ? "); // try (// RECEIVING THE NUMBER OF INPUTS // Scanner in1 = new Scanner(System.in)) { } int n = 0; // CREATING SPACE FOR THE TEMPERATURE INPUTS int temperatures[] = new int[n]; // CONTROL VARIABLE OF THE WHILE LOOP int itr = 0; // INITIALIZING THE AVERAGE VALUE TO 0 double average = 0.0; // RUN LOOP FOR N TIMES while (itr < n) { // PROMPT AND RECEIVING THE INPUT TEMPERATUREOF THAT DAY System.out.print("Enter the temperature for day " + (itr + 1) + " ... "); // scan = null; temperatures[itr] = scan.nextInt(); // ADDING THE VALUE TO average IN ORDER TO GET SUMMATION average += temperatures[itr]; // INCREATSING THE CONTROL VARIABLE BY 1 ++itr; // INSERTS NEW LINE System.out.print(" "); } // CALCULATING THE average FROM THE SUMMATION average /= (double) n; // DISPLAYING THE average System.out.print("The average daily temperature is ... " + average); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
