Results 1 to 4 of 4
- 05-09-2012, 02:56 AM #1
Member
- Join Date
- May 2012
- Posts
- 1
- Rep Power
- 0
I think I'm stuck in an infinite loop...
First off, super sorry if there's a specific way of posting code. Someone please correct me if this isn't the right way. But everytime I try to run the program, it freezes after the user puts in their number. I'm sure you guys know how frustrating it is, but if you don't mind helping a new Java-guy I would DEFINITELY appreciate it!Java Code:import acm.program.*;
Java Code:/*Write a program that reads in a positive integer N and then calculates an displays the sum of the first N odd integers.for example: if user puts in 4, answer should be 16 since 1+3+5+7=16; */ public class OddNumTotal extends ConsoleProgram{ public void run(){ int UserNum = readInt("Please enter in a positive number: "); int SumOddNums = 0; int Start = 1; int OddNumCount = 0; while(OddNumCount <= UserNum){ while(Start % 2 !=0){ SumOddNums += Start; OddNumCount++; Start++; if(Start % 2 == 0){ Start++; } } } println(SumOddNums); } }Last edited by pbrockway2; 05-09-2012 at 03:29 AM. Reason: code tags added
- 05-09-2012, 03:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: I think I'm stuck in an infinite loop...
No problem - what you do is put [code] at the start of the code and [/code] at the end. It's a good idea to use (a moderate number of) spaces to indent code as tabs can be rather wide and unpredictable when rendered on a web page like this.sorry if there's a specific way of posting code
- 05-09-2012, 03:47 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: I think I'm stuck in an infinite loop...
It's a good idea to follow Java coding conventions and begin variables with a lowercase letter: userNum, sumOddNums etc. Also, bear in mind that most here won't have the acm package. The use of this non standard package in schools is well intentioned but your teachers ought to be aware (or be made aware) of the problems it causes as you try and communicate with others.
But to address your problem... It would be good to know which while loop you are stuck in. In fact it would be good to know the values of especially oddNumCount and start as these are the things that get you out of the loops. (or, rather, aren't getting you out of the loops...) For that use System.out.println()
Java Code:/* * Write a program that reads in a positive integer N and then calculates and * displays the sum of the first N odd integers. For example: if user puts in 4, * answer should be 16 since 1+3+5+7=16. */ public class OddNumTotal extends ConsoleProgram { public void run(){ int UserNum = readInt("Please enter in a positive number: "); int sumOddNums = 0; int start = 1; int oddNumCount = 0; while(oddNumCount <= userNum){ while(start % 2 !=0) { sumOddNums += start; oddNumCount++; start++; if(start % 2 == 0){ start++; } println("End of inner loop, start=" + start); println(" will continue if it's odd"); } println("End of outer loop, oddNumCount=" + oddNumCount); println(" will continue if it's < " + userNuma); } println(sumOddNums); } }
- 05-09-2012, 04:04 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: I think I'm stuck in an infinite loop...
Make sure that your while loop have a correct termination condition so it will exit from the while loop at the time you want it to exit. As pbrockway2 said, you can print out the value of the variable you used in the condition expression of your while loop. If you use an IDE you can debug your code line by line.
As a suggestion, because you already know how many times you have to do a loop. In this case based on the number supplied by user. You can simply use a for loop instead of while loop.Website: Learn Java by Examples
Similar Threads
-
Stuck in Infinite Loop, Please!
By jakaldama in forum New To JavaReplies: 14Last Post: 03-27-2012, 06:53 PM -
Infinite loop
By F.S. in forum New To JavaReplies: 3Last Post: 03-09-2012, 12:52 PM -
Stuck in a infinite loop
By Pojahn_M in forum New To JavaReplies: 8Last Post: 08-23-2011, 02:17 AM -
how to end infinite loop
By search4survival in forum New To JavaReplies: 14Last Post: 10-25-2010, 08:59 AM -
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02-22-2010, 01:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks