Results 1 to 7 of 7
Thread: Loop question
- 02-07-2011, 07:05 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
Loop question
Hey all.
I been given this code:-
Its asks how would the second loop change if the program allowed the x to be 1.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!"); } }
I thought that it could possibly be that change
toJava Code:while (x != 1);
but it just produces a line of 1->4->2 without stopping.Java Code:while (x >=1);
Any help appreciated! :)
- 02-07-2011, 08:58 PM #2
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
- 02-07-2011, 09:56 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 10
- Rep Power
- 0
What code did you use?
Code I tried using:
This just prints = 1->4->2 repeatedly until i terminate the program, I dont know if im missing something?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!"); } }
Thanks
- 02-07-2011, 09:59 PM #4
I assigned values to x manually.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);
- 02-07-2011, 10:00 PM #5
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.
- 02-07-2011, 11:19 PM #6
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.
- 02-08-2011, 02:26 AM #7
Similar Threads
-
loop question
By ccie007 in forum New To JavaReplies: 22Last Post: 08-15-2010, 08:29 PM -
for Loop with Yes/No Question! help..please!
By mastercrimson in forum New To JavaReplies: 8Last Post: 06-02-2010, 05:08 PM -
Question about for loop..
By sivakumar_sakam in forum New To JavaReplies: 4Last Post: 05-15-2009, 11:23 PM -
nested for loop question
By javabob in forum New To JavaReplies: 3Last Post: 05-20-2008, 11:00 PM -
Question regarding foreach loop...
By theonlywalks in forum New To JavaReplies: 2Last Post: 03-15-2008, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks