Results 1 to 9 of 9
Thread: Count down
- 03-14-2011, 02:56 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
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.
- 03-14-2011, 03:06 PM #2
Im unsure of the exact maths required - you need to work it out - but instead of
you should useJava Code:for(int j = 1; j <= i; 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!Java Code:for(int j = 1; j <= i / 2; j++) { System.out.print(" "); }
- 03-14-2011, 03:13 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
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
- 03-14-2011, 03:21 PM #4
Apologies, the formatting threw me off the obvious error!
Your problem is obviously in this loop:
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).Java Code:for(int k = n; k >= i; k--) { System.out.print( k ); }
The value you are starting from changes each time, ill let you think about what to set it to.
PS use the [noparse] [/noparse] tags to display tags ;-)Java Code:for (int k = ???; k > 0; k--){ System.out.print( k ); }
- 03-14-2011, 03:40 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Ok so ??? is n--, right?
Now I get
what am I messing up on to have it finish out the 321, 21, 1?Java Code:654321 54321 4321
Last edited by tj_wolf2; 03-14-2011 at 03:46 PM.
- 03-14-2011, 03:48 PM #6
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 [/]?
- 03-14-2011, 03:59 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
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.
- 03-14-2011, 04:50 PM #8
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:
Note i've changed the first two statements in the for loop.Java Code:for(int i = 0; i < n; i++)
The first way is just as simple, but is less efficient (one or two more clock ticks (gasp))
Java Code:k = n - i + 1;
- 03-14-2011, 05:07 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Line Count
By tim in forum NetBeansReplies: 1Last Post: 08-02-2009, 04:41 PM -
Count on cantor help!?
By dianar17 in forum New To JavaReplies: 1Last Post: 04-06-2009, 06:22 AM -
to get count value as a variable
By arunkumarinfo in forum JDBCReplies: 2Last Post: 03-30-2009, 01:32 AM -
how we can get the count value in a variable
By arunkumarinfo in forum JDBCReplies: 4Last Post: 01-23-2009, 09:39 PM -
Getting row count
By Java Tip in forum Java TipReplies: 0Last Post: 02-11-2008, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks