Thread: Java SE7... halp!
-
March 9th, 2012, 04:40 PM #1Junior Member
- Join Date
- Nov 2010
- Location
- Northeast
- Posts
- 14
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
-
March 9th, 2012, 06:04 PM #2
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; } }
-
March 9th, 2012, 06:26 PM #3Junior 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...
-
March 9th, 2012, 06:28 PM #4
How many times is it asking then? Just once?
-
March 9th, 2012, 06:43 PM #5
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.
-
March 9th, 2012, 07:12 PM #6Junior 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
-
March 9th, 2012, 07:14 PM #7Junior 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.
-
March 9th, 2012, 07:15 PM #8
Glad to be of help (considering I haven't done any programming at all since about '05).
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
New to video editting...halp!
By RedFury in forum Multimedia and AudioReplies: 1Last Post: September 25th, 2008, 11:55 PM -
Xbox360 HALP! Disk problem
By Prince Lalijama in forum General Tech DiscussionReplies: 12Last Post: December 20th, 2006, 10:06 PM -
Launching Java webpages with different Java settings?
By LostBok in forum Webmastering and ProgrammingReplies: 1Last Post: December 11th, 2006, 03:38 AM -
Slooooooooow web page loading in XP Pro...halp
By Hopper in forum Applications and Operating SystemsReplies: 6Last Post: February 17th, 2003, 08:17 PM



LinkBack URL
About LinkBacks



Reply With Quote

It can help built-in graphics, but PS is probably hampered by a lack of features built in, rather than the lack of memory available for graphics. The built-in graphics shares the main memory, but is...
Does any graphics card work on any...