Results 1 to 4 of 4
  1. #1
    mtliendo is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default I think I'm stuck in an infinite loop...

    Java Code:
    import acm.program.*;
    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:
    /*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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: I think I'm stuck in an infinite loop...

    sorry if there's a specific way of posting code
    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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);
        }
    }

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

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

Similar Threads

  1. Stuck in Infinite Loop, Please!
    By jakaldama in forum New To Java
    Replies: 14
    Last Post: 03-27-2012, 06:53 PM
  2. Infinite loop
    By F.S. in forum New To Java
    Replies: 3
    Last Post: 03-09-2012, 12:52 PM
  3. Stuck in a infinite loop
    By Pojahn_M in forum New To Java
    Replies: 8
    Last Post: 08-23-2011, 02:17 AM
  4. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  5. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •