Results 1 to 8 of 8
Thread: [Q] Spacing Help
- 02-08-2011, 01:15 AM #1
Member
- Join Date
- Jan 2011
- Location
- Ohio
- Posts
- 11
- Rep Power
- 0
[Q] Spacing Help
Hello! I am having serious issues with trying to get the spacing on my homework to look right. I have to print out a triangle that looks like this:
Java Code:1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 16 8 4 2 1 1 2 4 8 16 32 64 32 16 8 4 2 1 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
PS: Here is the code I have so far. The object was to make it using for loops.
Java Code:public static void main(String[] args) { //Declare variables int lengthOfLine; //Get input from user Scanner inputFromUser = new Scanner(System.in); System.out.print("Enter a positive number: "); int height = inputFromUser.nextInt(); lengthOfLine = 1; for (int count = 0; count < height; count++) { for (int numberOfSpacesForLine = 1; numberOfSpacesForLine < height - count; numberOfSpacesForLine++) { if () System.out.print(" "); } int numberToPrint = 1; for (int loopCount = 0; loopCount < lengthOfLine; loopCount++) { System.out.print(numberToPrint); if (loopCount < ((lengthOfLine - 1) / 2.0)) { numberToPrint *= 2; } else { numberToPrint /= 2; } } lengthOfLine += 2; System.out.println(); } } }
- 02-08-2011, 01:19 AM #2
You can use String.format or the printf method.
- 02-08-2011, 01:57 AM #3
Member
- Join Date
- Jan 2011
- Location
- Ohio
- Posts
- 11
- Rep Power
- 0
It should be printf(%5d , numberToBePrinted); right?
- 02-08-2011, 01:58 AM #4
Did you try it?
- 02-08-2011, 02:09 AM #5
Member
- Join Date
- Jan 2011
- Location
- Ohio
- Posts
- 11
- Rep Power
- 0
No, I haven't gotten a chance. I'm delivering pizzas and I had a chance to post my question real quick at the beginning of my shift. I did ask that badly. I meant *should* that work? If not, I don't expect you to just give me the answer. I was just hoping I'm going the right direction with it. Thanks so much for your help!
- 02-08-2011, 02:19 AM #6
Well it looks like it might be correct but the best way to find out is when you get on the computer and test if it does what you want.
-
You shouldn't be asking these questions if you're not able to first test them on your computer. We are no substitute for a Java compiler.
- 02-08-2011, 02:44 AM #8
Member
- Join Date
- Jan 2011
- Location
- Ohio
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Spacing/Alignment problem with java 2D printing
By myselfmayur in forum Java 2DReplies: 2Last Post: 06-25-2010, 04:37 PM -
digit spacing
By puk284 in forum New To JavaReplies: 3Last Post: 06-09-2009, 03:49 PM -
Problem with margins/spacing
By joseche in forum AWT / SwingReplies: 2Last Post: 12-26-2008, 09:22 PM -
Spacing buttons on a panel
By 2o2 in forum New To JavaReplies: 8Last Post: 10-20-2008, 11:44 AM -
Help with spacing in java
By barney in forum New To JavaReplies: 1Last Post: 07-31-2007, 09:03 PM
Bookmarks