The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Temperature

I am brand new to java programming and am having a heck of a time trying to figure this out. I need to create a weekly temperature program that asks the user to input the day, the high temperature, and the low temperature for that day. The total high and total low will be calculated as I enter the high and low temperatures. After all seven days have been entered, an average high and average low will be calculated for the week. I need to use a for loop, while loop, or a do-while loop to enter the temps.
I need to display the output as:

The average high temp of the week: (high average).
The average low temp of the week: (low average).

I have been working at this program trying to figure out what I need to do for hours. I am totally lost. Any help would be appreciated.

Re: Temperature

I'm not sure sure why you are asking the user for the day. The day name doesn't play any role in the question as you stated it.

Also where does the 3 come from? There are seven days in the week, so I would expect the loop to go around 7 times.

What are high and low supposed to be? You are setting them to be the values entered by the user on a particular day. But, that case you will need a couple of more variables for the totals so that "The total high and total low will be calculated". Basically declare these variables then, inside the loop add on that day's values.

Re: Temperature

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.