Results 1 to 8 of 8
  1. #1
    maniku is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

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

    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 + " ");
            }
        }
    }
    It works, nearly:

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

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

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

  3. #3
    maniku is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    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--;
    }
    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?

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by maniku View Post
    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?
    Just realized I misread your problem -.-
    Last edited by Solarsonic; 05-25-2011 at 12:55 AM.

  5. #5
    maniku is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    Just realized I misread your problem -.-
    No worries, thanks for trying to help though.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Try debugging your code by adding some printlns
    For example:

    System.out.println("<i=" + i + ", num=" + num + ">");

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

    Default

    Java Code:
    for (int i = 1; i < num; i++) {
                int a = num--;
    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

Similar Threads

  1. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-14-2010, 12:14 AM
  2. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-14-2010, 12:13 AM
  3. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM
  4. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 03:15 AM
  5. Log4j not printing out file and line number
    By devin in forum Java Servlet
    Replies: 2
    Last Post: 03-05-2009, 04:16 PM

Tags for this Thread

Posting Permissions

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