Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Temperature

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    This is what I have so far:

     
    import java.util.Scanner;
     
    public class assignment3a_Temp
    {
      public static void main(String[]args)
      {
        Scanner keyboard = new Scanner(System.in);
     
        int low = 0, high = 0, count, avghigh = high/7, avglow = low/7;
        String day;
     
    //*************************************************
     
        for(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();
     
          System.out.println("Enter day " + count + " high temperature.");
          high = keyboard.nextInt();
        }
          System.out.println("The average low for the week is " + avglow);
          System.out.println("The average igh for the week is " + avghigh);
          {
        }
      }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Program83 (June 25th, 2013)

  4. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Temperature

    Sorry, this is my very first time on this site and my very first program. I fixed it to read better and made a few changes to the code itself. How do I declare the variables for the totals?

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

Similar Threads

  1. temperature converter
    By pranavk26 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 11th, 2013, 06:38 AM
  2. Conversion of the temperature degree
    By alialomran in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 08:16 AM
  3. Temperature Conversion Chart
    By cb12991 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 8th, 2012, 12:11 AM
  4. NEED HELP WITH TEMPERATURE
    By Dracer in forum AWT / Java Swing
    Replies: 5
    Last Post: December 11th, 2010, 03:12 PM
  5. Converting temperature program
    By ixjaybeexi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2009, 07:27 PM