Results 1 to 7 of 7

Thread: Loop question

  1. #1
    rich123 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default Loop question

    Hey all.

    I been given this code:-

    Java Code:
    import java.util.*;
    
    public class Syr {
    	public static void main(String args[]) {
    		Scanner sc = new Scanner(System.in);
    		int x;
    
    		System.out.println("Enter a number to be iteratively mapped using the Syracuse function.");
    		do {
    			System.out.println("(Please make sure it is greater than 1.)");
    			x = sc.nextInt();
    		} while (x <= 1);
    
    		System.out.print(x);
    		do {
    			if ((x % 2) == 0)
    				x = x / 2;
    			else
    				x = 3 * x + 1;
    			System.out.print("->" + x);
    		} while (x != 1);
    		System.out.println();
    		System.out.println("Our hypothesis is true for this value!");
    	}
    }
    Its asks how would the second loop change if the program allowed the x to be 1.
    I thought that it could possibly be that change
    Java Code:
    while (x != 1);
    to

    Java Code:
    while (x >=1);
    but it just produces a line of 1->4->2 without stopping.

    Any help appreciated! :)

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,338
    Rep Power
    9

    Default

    Your code works for me, you sure?
    x = 1
    result:
    ->4->2->1
    x = 3
    result:
    ->10->5->16->8->4->2->1
    x = 4
    result:
    ->2->1

  3. #3
    rich123 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    What code did you use?

    Code I tried using:

    Java Code:
    import java.util.*;
    
    public class Syr {
    	public static void main(String args[]) {
    		Scanner sc = new Scanner(System.in);
    		int x;
    
    		System.out.println("Enter a number to be iteratively mapped using the Syracuse function.");
    		do {
    			System.out.println("(Please make sure it is greater than 1.)");
    			x = sc.nextInt();
    		} while (x <= 0);
    
    		System.out.print(x);
    		do {
    			if ((x % 2) == 0)
    				x = x / 2;
    			else
    				x = 3 * x + 1;
    			System.out.print("->" + x);
    		} while (x >=1);
    		System.out.println();
    		System.out.println("Our hypothesis is true for this value!");
    	}
    }
    This just prints = 1->4->2 repeatedly until i terminate the program, I dont know if im missing something?

    Thanks

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,338
    Rep Power
    9

    Default

    Java Code:
    int x = 7;
    do {
        if ((x % 2) == 0)
            x = x / 2;
        else
            x = 3 * x + 1;
        System.out.print("->" + x);
    } while (x >=1);
    I assigned values to x manually.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,338
    Rep Power
    9

    Default

    I also suggest you clean and rebuild your program. Especially in eclipse, sometimes, it gets the current code and last build confused and executes some old version of the project.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    I'm not sure what code you are running quad but if you change the condition to x >= 1 then it becomes an endless loop.

    Why? If you examine the if statement carefully you will see that it is impossible for x to ever be less than 1.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,338
    Rep Power
    9

    Default

    You know, I don't know what I ran either. I must have fixed it by accident when I pasted it in. I'm sorry! :P

Similar Threads

  1. loop question
    By ccie007 in forum New To Java
    Replies: 22
    Last Post: 08-15-2010, 08:29 PM
  2. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 05:08 PM
  3. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-15-2009, 11:23 PM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  5. Question regarding foreach loop...
    By theonlywalks in forum New To Java
    Replies: 2
    Last Post: 03-15-2008, 07:15 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
  •