+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Junior Member
    Join Date
    Nov 2010
    Location
    Northeast
    Posts
    14

    Red face Java SE7... halp!

    My code compiles and runs, but it doesn't execute as many times as I need it to. I need it to ask for input 12 times (for each month). What am I missing? Here's the code:

    import java.util.Scanner;

    public class HighestTemp {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("Enter the temperature for the month:");
    double highestValue = in.nextDouble();
    int highestMonth = 1;
    for (int currentMonth = 2; currentMonth <= 12; currentMonth++) {
    double nextValue = in.nextDouble();
    if (nextValue > highestValue) {
    highestValue = nextValue;
    highestMonth = currentMonth;

    }
    System.out.println("The highest value is: " + highestValue +
    " and the month of the highest value is: " + highestMonth);

    }
    }
    }

    THANKS FOR ANY HELP!! <3

  2. #2
    ph34r t3h g04t Whir's Avatar
    Join Date
    Oct 2001
    Location
    Kingsford, MI
    Posts
    30,075
    Blog Entries
    7
    My JAVA is super rusty, but I think you should be doing all of the math inside the loop rather than starting with an input value outside the loop.

    int highestMonth = 1;
    double highestValue = 0;
    for (int currentMonth = 1; currentMonth <= 12; currentMonth++) {
    System.out.println("Enter the temperature for the month:");
    double inputValue = in.nextDouble();
    if (inputValue > highestValue) {
    highestValue = inputValue;
    highestMonth= currentMonth; } }

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Location
    Northeast
    Posts
    14
    I changed it around a bit, like you said, and it did the same thing. Also, the book I have gave me the loop so I have to assume at least that much is right. It's confusing! I have another assignment that won't stop giving output! lol it's only supposed to randomly select 100 numbers between 1 and 1000, but it's just going and going and going...

  4. #4
    ph34r t3h g04t Whir's Avatar
    Join Date
    Oct 2001
    Location
    Kingsford, MI
    Posts
    30,075
    Blog Entries
    7
    How many times is it asking then? Just once?

  5. #5
    ph34r t3h g04t Whir's Avatar
    Join Date
    Oct 2001
    Location
    Kingsford, MI
    Posts
    30,075
    Blog Entries
    7
    Also, I believe

    System.out.println("The highest value is: " + highestValue +
    " and the month of the highest value is: " + highestMonth);

    should be one } further down. As you have it, it's inside the loop's code and will execute every time. That might be why it seems like it's only working once. Notice how I put the } } after my loop.

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Location
    Northeast
    Posts
    14
    ~gasp~ That fixed it!!!!
    I can't believe it was that simply (being inside the loop). I am SO grateful

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Location
    Northeast
    Posts
    14
    ...that also explains why it kept saying it was running when I thought it was finished.

  8. #8
    ph34r t3h g04t Whir's Avatar
    Join Date
    Oct 2001
    Location
    Kingsford, MI
    Posts
    30,075
    Blog Entries
    7
    Glad to be of help (considering I haven't done any programming at all since about '05).

Quick Reply Quick Reply

If you are already a member, please login above.

What is the color of the sky?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. New to video editting...halp!
    By RedFury in forum Multimedia and Audio
    Replies: 1
    Last Post: September 25th, 2008, 11:55 PM
  2. Xbox360 HALP! Disk problem
    By Prince Lalijama in forum General Tech Discussion
    Replies: 12
    Last Post: December 20th, 2006, 10:06 PM
  3. Launching Java webpages with different Java settings?
    By LostBok in forum Webmastering and Programming
    Replies: 1
    Last Post: December 11th, 2006, 03:38 AM
  4. Slooooooooow web page loading in XP Pro...halp
    By Hopper in forum Applications and Operating Systems
    Replies: 6
    Last Post: February 17th, 2003, 08:17 PM

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright 2014 All Enthusiast, Inc