// Importing java utilites are necessary when using objects such as Scanner or Random.
import java.util.*;
// All programs require a class. Conventionally, public class NameHere{} is used.
public class TemperatureAverage
{
// This is the main method, and will usually look the same as written below.
public static void main(String[] args)
{
// Printing is as you see below. If you wish to print a message, enclose it in quotations.
System.out.println("Enter a temperature for 5 days.");
// You must initating a few variables BEFORE you have to use them. It's easiest to do all of the method variables in one block so you can refer to them quickly
double[] temps = new double[5]; // Double will be used so you can have decimal places. This is conventional, since you want to find the average-- which is often a decimal number
double totalTemp = 0; // It's easiest to set all variables to the double if your result is going to be double. This way, you don't have to cast integers as doubles later on.
double average = 0; // Setting your variables to 0 when you decalre them is conventional. This is called initating the variables, and it will be updated later as you perform your calculations
// This line of code below will create a Scanner object. That's it. The object you created, called "scan" here, won't do anything yet until you tell it to.
Scanner scan = new Scanner(System.in);
// So begins the first for loop.
for(int i = 0; i < 5; i++) // Notice the format within the parameters. You're creating a new integer 'i' and setting it to 0. SEMICOLON! Then you're saying you want this loop to iterate 5 times before exiting. SEMICOLON! That's accomplished with the third bit of code, i++. This will add 1 to i at the END of each loop, after the body is executed.
{
// Here's the body. This will execute 5 times before i is no longer less than 5, which means the loop will exit when i = 5.
System.out.print("Day " + (i+1) + ": "); // NOTICE, I can have variables inside print statements, and they are not enclosed with quotations. ALSO NOTICE that you MUST separate quoted text from variables (and also variables from other variables, if you're printing two) with a + sign. This is cruitial. It's called "concatination", and without it, java won't know when a string ends and the variable begins.
// Also notice the use of 'i'. In the for loop, you create the variable 'i', and set it equal to 0. Therefore, in the above statement (0+1) will be printed since that's the value of i.
// Below, you see us opening the array, using 'i' as the index number. Since 'i' is set to 0, temps[i] is actually temps[0]. After the loop iterates once, 'i' will be equal 1 because of the i++ in the parameters. And so on.
temps[i] = scan.nextDouble();
// Therefore, the iterations will look like this:
// temps[i] = temps[0] = your input from the scanner object
// temps[i] = temps[1] = your next input from the scanner object
// ...
// temps[i] = temps[5] = your next input from the scanner object
// and the for loop will then exit because i = 5, and is not less than 5.
}
// Here's another for loop that we will use to open the temps[] array, and add each value, one at a time, to totalTemp.
for(int i = 0; i < 5; i++)
{
totalTemp += temps[i]; // NOTE the operator += is the same as totalTemp = temps[i] + totalTemp. So totalTemp is 0 initially, so this would look like 0 = temps[i]+0.
// The iterations will look like this:
// totalTemp += temps[i] = temps[0] = whatever you put in previously
// totalTemp (updated) += temps[i] = temps[1] = whatever you put in previously
// ...
// totalTemp (updated with temps[0 through 4] += temps[i] = temps[5] = whatever you put in previously
// And the loop ends because i = 5.
}
// Update the value of "average" with the totalTemp (the added value of all the temps you put in) divided by 5.
average = totalTemp/5;
// Print the average temperature.
System.out.println("The average of the temperatures is " + average);
}
}