5 Replies - 6026 Views - Last Post: 02 March 2011 - 07:16 PM Rate Topic: -----

#1 NeedJavaHelp22322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-March 11

Star Pyramid Problem

Posted 02 March 2011 - 06:23 PM

case 'a':
	System.out.println("The design is");
	for (int row = 1; row <= num; row++)
        {
	      for (int star = 1; star <= row; star++)
		System.out.print("*");

		System.out.println();
	}
	break;


So I am writing a program that outputs pyramids of stars. The above code outputs a shape like this:
*
**
***
etc.

However, I need to write a code just like this but reversed so it comes out like this and :
---*
--**
-***
****
etc.

the etc. is based off the inputted number...And the (-) are spaces, it keeps aligning it to the left in my post.

Can anyone help me? I don't know where to even begin...Thanks.

This post has been edited by NeedJavaHelp22322: 02 March 2011 - 06:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Star Pyramid Problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Star Pyramid Problem

Posted 02 March 2011 - 06:32 PM

Before printing your * think at the number of space you need
somethink like int nbSpace = num - row; and then
for(int space = 0; space < nbSpace; space++)
System.out.print(" ");
Was This Post Helpful? 1
  • +
  • -

#3 NeedJavaHelp22322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-March 11

Re: Star Pyramid Problem

Posted 02 March 2011 - 06:39 PM

case 'c':
						System.out.println("The design is");
						for (int row = 1; row <= num; row++)
							{
								int nbspace = num - row;
								for (int space = 0; space < nbspace; space++)
									{
									System.out.print("");
									}
										for (int star = 1; star <= row; star++)
											System.out.print("*");

											System.out.println();
							}
					break;


I put it like this and it still has it left aligned. I understand how you're trying to designate a certain amount of spaces, but maybe I'm just placing it in the wrong spot...
Was This Post Helpful? 0
  • +
  • -

#4 NeedJavaHelp22322  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-March 11

Re: Star Pyramid Problem

Posted 02 March 2011 - 06:47 PM

Nevermind, it ended up being as simple as placing a space in between the quotes. Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Star Pyramid Problem

Posted 02 March 2011 - 06:49 PM

System.out.print(" ");
not
System.out.print("");
:)
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Star Pyramid Problem

Posted 02 March 2011 - 07:16 PM

Oups you Ninjad me :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1