Results 1 to 9 of 9

Thread: Count down

  1. #1
    tj_wolf2 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Count down

    How can I get my code to count down like this
    6 5 4 3 2 1
    __5 4 3 2 1
    ___4 3 2 1
    ___ 3 2 1
    _____ 2 1
    ______ 1
    I am getting this
    654321
    _65432
    __6543
    ___654
    ____65
    _____6

    Here is my code.


    import java.util.Scanner;
    public class genTower
    {
    public static void main(String args[])
    {
    Scanner input = new Scanner(System.in);
    int n=0;
    do {
    System.out.print("Enter a positive integer : "); n = input.nextInt();
    } while( n <= 0);
    System.out.println();
    for(int i = 1; i <=n; i++)
    {
    for(int j = 1; j <= i; j++)
    {
    System.out.print(" ");
    }
    for(int k = n; k >= i; k--)
    {
    System.out.print( k );
    }
    System.out.println();
    }
    }
    }
    Last edited by tj_wolf2; 03-14-2011 at 02:58 PM.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Im unsure of the exact maths required - you need to work it out - but instead of
    Java Code:
    for(int j = 1; j <= i; j++)
    {
    System.out.print(" ");
    }
    you should use
    Java Code:
    for(int j = 1; j <= i / 2; j++)
    {
    System.out.print(" ");
    }
    In future please use [code] [/code] tags - it preserves spaces and makes code much more readable. It would have come in handy when describing your output, as its been horribly mangled!

  3. #3
    tj_wolf2 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    My main concern is how to get each row to drop the highest number e.i. 321, 21, 1 instead of dropping the lowest 321, 32, 3. And Ill remember to use the . Thanks

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Apologies, the formatting threw me off the obvious error!

    Your problem is obviously in this loop:
    Java Code:
    for(int k = n; k >= i; k--) {
    System.out.print( k );
    }
    Think about what you are counting from and to. 6 - 1, 5 -1, 4-1 etc. So k needs to be greater than 0 (or greater or equal to 1).
    The value you are starting from changes each time, ill let you think about what to set it to.

    Java Code:
    for (int k = ???; k > 0; k--){
        System.out.print( k );
    }
    PS use the [noparse] [/noparse] tags to display tags ;-)

  5. #5
    tj_wolf2 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Ok so ??? is n--, right?
    Now I get
    Java Code:
    654321
     54321
      4321
    what am I messing up on to have it finish out the 321, 21, 1?
    Last edited by tj_wolf2; 03-14-2011 at 03:46 PM.

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Close, but not quite. n should not be changed.
    You are only getting three outputs because on the fourth run through, i is four and n is three, so the outer for loop terminates.

    k needs to be initiated at 6, then 5, then 4 etc. Or we can call it n - 1, n -2, n - 3 etc.
    So we need some variable which counts how many times we have initialised k. Personally, I'd call it i, wouldnt you?

    k = n - i;

    btw, what did you type when trying to do the tags? How did you get [] and [/]?

  7. #7
    tj_wolf2 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Alright getting close. With k = n - i it takes 1 from n right away so if I type 6 it starts 5 right? I need it to start with 6.
    I first put [] [/] and that didnt work then I changed to [noparse] [/noparse] then switched to [code] [/code] to align it right.

  8. #8
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You've got two options to rectify that.
    First: add one to the result
    Second: Deduct one from i
    Personally, I recommend the second option - its slightly more efficient, and is "the C way". Most things in C are 0 based - for instance, the first position in the array is located at [0].

    So, to correct this you would change the for loop:
    Java Code:
    for(int i = 0; i < n; i++)
    Note i've changed the first two statements in the for loop.

    The first way is just as simple, but is less efficient (one or two more clock ticks (gasp))
    Java Code:
    k = n - i + 1;

  9. #9
    tj_wolf2 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Ok that works out. Thanks for all the help the code and how to post.

Similar Threads

  1. Line Count
    By tim in forum NetBeans
    Replies: 1
    Last Post: 08-02-2009, 04:41 PM
  2. Count on cantor help!?
    By dianar17 in forum New To Java
    Replies: 1
    Last Post: 04-06-2009, 06:22 AM
  3. to get count value as a variable
    By arunkumarinfo in forum JDBC
    Replies: 2
    Last Post: 03-30-2009, 01:32 AM
  4. how we can get the count value in a variable
    By arunkumarinfo in forum JDBC
    Replies: 4
    Last Post: 01-23-2009, 09:39 PM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 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
  •