Results 1 to 8 of 8
- 05-24-2011, 11:06 PM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
What's missing in this java number printing exercise?
Hi there,
Working on the following exercise: write code which asks the user for upper limit num, then prints the numbers in the following sequence:
if num limit is e.g. 8, the printout has to be: 1 8 2 7 3 6 4 5.
My code:
It works, nearly:XML Code:import java.util.*; public class T59_Vuorotellen { private static Scanner input = new Scanner(System.in); public static void main(String[] args) { System.out.print("Give number: "); int num = Integer.parseInt(input.nextLine()); for (int i = 1; i < num; i++) { int a = num--; int b = i; System.out.print(b + " "); System.out.print(a + " "); } } }
-If num limit is even, it behaves as supposed to.
-If num limit is odd, it leaves the last number to be printed out. E.g. if limit is 9, it prints 1 9 2 8 3 7 4 6 when it SHOULD print 1 9 2 8 3 7 4 6 5
If I replace < with <= or add +1 to num in the for loop, the result is still as should be with even nums, and with odd numbers the last number IS printed but twice. Scratching my head here.
- 05-24-2011, 11:35 PM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Java Code:int increasingNum=1; // initialization int decreasingNum=9; // initialization while(increasingNum<9 && decreasingNum>1) { System.out.print(increasingNum + " "); System.out.print(decreasingNum + " "); increasingNum++; decreasingNum--; }
- 05-24-2011, 11:45 PM #3
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Thanks but that's not it. It prints: 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2
The code is supposed to stop when it has printed all the numbers between 1 and the user input limit. I appreciate the suggestion, however was hoping to find whatever is the bit that is missing from the code that I have?
- 05-24-2011, 11:50 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 05-25-2011, 12:05 AM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
- 05-25-2011, 12:42 AM #6
Try debugging your code by adding some printlns
For example:
System.out.println("<i=" + i + ", num=" + num + ">");
- 05-25-2011, 01:39 AM #7
The loop iterates while i is less than num. Your problem is that inside the loop you change the value of num thus your loop never executes the required number of iterations. Since i is increasing by 1 and num is decreasing by 1 the condition is met twice as fast as expected.Java Code:for (int i = 1; i < num; i++) { int a = num--;
- 05-25-2011, 02:35 AM #8
Similar Threads
-
Exercise for java 3d
By armiri in forum Java 2DReplies: 2Last Post: 05-13-2010, 11:14 PM -
Exercise for java 3d
By armiri in forum Java SoftwareReplies: 3Last Post: 05-13-2010, 11:13 PM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
printing number of pos/neg numbers input?
By shroomiin in forum New To JavaReplies: 12Last Post: 09-25-2009, 02:15 AM -
Log4j not printing out file and line number
By devin in forum Java ServletReplies: 2Last Post: 03-05-2009, 03:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks