Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: help with number game using nested for and doWhile loop

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with number game using nested for and doWhile loop

    Trying to create a game that returns the amount of times it takes the all numbers between a beginning input and ending input to work through a loop before it becomes zero. Any ideas on why I keep getting an infinite loop?

     
    import java.util.Scanner;
     
    class Suite {
     
        private static Scanner clavier = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            int debut;
            do {
                System.out.print("de (>= 1) ? ");
                debut = clavier.nextInt();
            } while (debut < 1);
     
            int fin;
            do {
                System.out.print("a (>= " + debut + ") ? ");
                fin = clavier.nextInt();
            } while (fin < debut);
     
            for (int x = debut; x <= fin; ++x)	{
            int k = 0;
            	do	{
            		if (x % 3 == 0)	{
            			x =+ 4;
            			++k;
            		}
            		if (x % 3 != 0 && x % 4 == 0)	{
            			x = x / 2;
            			++k;
            		}
            		if (x % 3 != 0 && x % 4 != 0)	{
            			x = x - 1;
            			++k;
            		}
            	}	while (x != 0);
     
            	System.out.println(x + " --> " + k);
            }
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with number game using nested for and doWhile loop

    Modifying the loop control variable inside the loop is a dangerous game and is surely causing the infinite loop. If using the loop control variable is necessary, copy it to another variable each time through the loop before modifying it.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Niu (October 18th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with number game using nested for and doWhile loop

    Thanks for the insight. I'm a newbie to programming, so am not even sure which one is the "loop control variable." I'm modifying k in order to count how many iterations are necessary to attain zero. I'm modifying x as ordered by the game based on properties of the number. i'm looking into how to copy either of these variables into another variable, but am unsure of how to do so..

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with number game using nested for and doWhile loop

    I should have specified. The for loop control variable, x, is the one of concern. Since the for loop will continue as long as x <= fin, you've probably done something in the do/while loop that limits x to be less than fin, so the for loop runs forever.

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with number game using nested for and doWhile loop

    ah yes...especially considering x becomes 0 and fin is greater than 1! thanks again. going to create another variable now...

Similar Threads

  1. nested while loop
    By Amruta N in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2013, 12:35 AM
  2. Using a Loop and Nested Ifs to Count Number of Vowels in a String
    By Potat in forum Loops & Control Statements
    Replies: 17
    Last Post: February 21st, 2013, 09:35 PM
  3. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  4. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 03:49 PM
  5. Replies: 4
    Last Post: September 6th, 2012, 05:29 AM