Results 1 to 4 of 4
Thread: FOR Loop
- 05-13-2011, 01:32 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
FOR Loop
Having trouble with my For Loop. It keeps Looping.:confused:
Java Code:import java.util.Scanner; public class tempcalc { /** * @param args */ public static void intro() { System.out.println("Welcome to TempCalc."); System.out.println("Reading temperature values for 12 months."); } public static void temp() { Scanner input = new Scanner(System.in); int tempgroup[]; int index; int month; tempgroup = new int [12]; for (index = 0; index < 12; index = index + 1) { for (month = 1; month < 13; month ++) { System.out.println("Please enter the value for month " + month); tempgroup[index] = input.nextInt(); } } } public static void main(String[] args) { intro (); temp (); } }
- 05-13-2011, 01:38 PM #2
That one should loop 156 times. Does it loop more than that?
EDIT: Correction, 144 times.
- 05-13-2011, 01:39 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Why are you using a nested loop? It wot go for ever, however; it will loop 12 * 12 times, or 144. I suspect you meant to only use a single for loop which gets the temp for each month.
- 05-13-2011, 01:44 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
You have a loop that fills the tempgroup array with 12 month values which is inside another loop that runs 12 times, so you fill (overwrite) the array 12 times (total of 144 values entered).
If you want to loop 12 times and prompt with the month number, remove the inner month loop and just use the index + 1 as the month number.
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks