# Star Pyramid Problem

Page 1 of 1

## 5 Replies - 10173 Views - Last Post: 02 March 2011 - 07:16 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=219902&amp;s=f29107febba7cc678ad66b684208445e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 NeedJavaHelp22322

Reputation: 0
• 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

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

Reputation: 8378
• Posts: 31,956
• 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(" ");

### #3 NeedJavaHelp22322

Reputation: 0
• 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...

### #4 NeedJavaHelp22322

Reputation: 0
• 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!

### #5 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Star Pyramid Problem

Posted 02 March 2011 - 06:49 PM

System.out.print(" ");
not
System.out.print("");

### #6 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Star Pyramid Problem

Posted 02 March 2011 - 07:16 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }