my dog learned polymorphism*
The moose likes Beginning Java and the fly likes I need help with code, to create a pyramid with asterisk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need help with code, to create a pyramid with asterisk" Watch "I need help with code, to create a pyramid with asterisk" New topic
Author

I need help with code, to create a pyramid with asterisk

scott daston
Greenhorn

Joined: Sep 28, 2007
Posts: 5
I am stuck on what code to use to create a pyramid of asterisks using for loops.....
What I come up with is half of the pyramid (looks left justify)
SOmeone please help!!!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

Show us the code you have so far, and we'll help you work it out.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
scott daston
Greenhorn

Joined: Sep 28, 2007
Posts: 5
This is what I have so far

for (int x=0; x<4; x++)
{
for (int y=0; y<=x; y++)
{
System.out.print ("*");
}
System.out.println();
}
scott daston
Greenhorn

Joined: Sep 28, 2007
Posts: 5


Ultimatly this is what its suppose to look like.

Thanks to anyone that can help me.

EDIT by mw: Added Code Tags to keep formatting.
[ September 28, 2007: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

To illustrate one way of approaching this, consider the following triangle.

Try describing the logic in English first. Then you will have a better idea of what you're trying to code. For example, row by row, the above triangle is...

3 spaces and 1 asterisk
2 spaces and 3 asterisks
1 space and 5 asterisks
0 spaces and 7 asterisks

If you're printing this row by row, you probably have a loop iterating each row. So it makes sense to try and relate what you're doing to the row number (a variable x).

When x is 0: 3 spaces, 1 asterisk
When x is 1: 2 spaces, 3 asterisks
When x is 2: 1 space, 5 asterisks
When x is 3: 0 spaces, 7 asterisks

So the question is: If you know x, how do you get the number of spaces and the number of asterisks?
scott daston
Greenhorn

Joined: Sep 28, 2007
Posts: 5
thats the thing i am stuck, I am not sure how to code that exactly...I know the logic of it but the coding is what i need.
please help
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by scott daston:
...I know the logic of it but the coding is what i need...

You know what the result should be, but do you know the logic detailed enough to code it?

For example, how can you express the number of spaces in terms of x? And how can you express the number of asterisks in terms of x?
scott daston
Greenhorn

Joined: Sep 28, 2007
Posts: 5
the thing is that I have been looking at this for hours and I can't seem to figure it out. Thanks for your help anyways!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When x is 0: 3 spaces, 1 asterisk
When x is 1: 2 spaces, 3 asterisks
When x is 2: 1 space, 5 asterisks
When x is 3: 0 spaces, 7 asterisks

Notice that the number of leading spaces is always (3 - x).

Now if you can also see how the number of asterisks relates to x, then you will have a good idea of what to code for each row.
swimdudea swimdudeb
Greenhorn

Joined: Mar 02, 2010
Posts: 1
This is for people who will be searching for this same exact problem next semester for Intro to Java. I found nothing useful when browsing sites like this that explained or showed you the code. DO NOT COPY THIS CODE AS IT IS COPYRIGHT INFRINGEMENT...THIS IS MY CODE!

[DELETED]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

swimdudea swimdudeb wrote:This is for people who will be searching for this same exact problem next semester for Intro to Java. I found nothing useful when browsing sites like this that explained or showed you the code. DO NOT COPY THIS CODE AS IT IS COPYRIGHT INFRINGEMENT...THIS IS MY CODE!


And there is a good reason for that. When a solution to a homework problem is posted -- people tend to not do the homework. And when people don't do homework, they don't learn.

Please help people learn. Thanks.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Siva Arun
Greenhorn

Joined: Feb 23, 2010
Posts: 28
Before start coding, always try to split the actions like below. (-) represents spaces here. This will speed up the coding and avoid confusions.



"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

swimdudea swimdudeb wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
wood burning stoves
 
subject: I need help with code, to create a pyramid with asterisk
 
Similar Threads
Pyramid
program correction
Need help with columns of Pyramid of numbers
The pyramid attacks!
Need help with right align for number pyramid