Don't be sorry - I mostly ask questions. It doesn't mean you did anything wrong.
How do I declare the variables for the totals?
The same way you declare the other variables:
int lowTotal;
int highTotal;
There are some good habits to get into about declaring variables:
- One per line for legibility
- Declare them where you are going to use them
- Don't give them an initial value unless you really mean them to have that value for some reason
Your code, but applying those habits (the comments are commentary and wouldn't go in the actual code)
public class assignment3a_Temp
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
// these will get their values inside the loop,
// and need not be assigned initial values here
int low;
int high;
String day;
// declare the total variables here because the question
// says the total should be calculated inside the loop
// Notice that these variables should be assigned an initial value
// of zero because the total *is* zero to begin with
//*************************************************
// count is declared as part of the for statement
for(int count = 1; count <= 7; count++)
{
System.out.print("Enter day " + count + " of the week.");
day = keyboard.nextLine();
System.out.println("Enter day " + count + " low temperature.");
low = keyboard.nextInt();
// update lowTotal
System.out.println("Enter day " + count + " high temperature.");
high = keyboard.nextInt();
// update highTotal
}
// now declare avghigh and avglow
// and make them equal to the total divided by seven
System.out.println("The average low for the week is " + avglow);
System.out.println("The average igh for the week is " + avghigh);
}
}
(Another option would be to have
low and
high represent the low and high totals so far. That would simplify the code because you wouldn't need as many variables, but you would have to change "low=keyboard.nextInt()" so that it
added whatever nextInt() returned to
low. It wasn't really clear from your code whether, eg, you meant
low to be that day's low value or the total of the low values so far. That's why I asked. It doesn't matter which you choose, but you should choose and be consistent.)
Notice that you can't assign anything useful to the average variables until
after the loop has finished.