I'm having a lot of trouble understanding an assignment we were given today. It has to be written using only loops. I'm finding it a little hard to comprehend right now, so just a bit of guidance on where to start would be great.
I understand that the program would start at the top left so, everything would rely on nested loops.

The user is asked to enter:
An X,Y coordinate for the upper vertex
And the length of the pyramid

I.e. If a user entered 15,15 and 9

The result:

``````20+
|
|
|
|
15             *
|             ***
|            *****
|           *******
|          *********
10          *******
|            *****
|             ***
|              *
|
5
|
|
|
|
+----+----+----+----+----+----+----+---->
0``````

Thanks

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by JamesCherrill

So far, this is what I have:

``````import java.util.Scanner;

public class Assignment4
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int count, xCoord, yCoord, length;

System.out.println("Please enter the X Coordinate: ");
xCoord = input.nextInt();

System.out.println("Please enter Y Coordinate: ");
yCoord = input.nextInt();

System.out.println("Please enter the length: ");
length = input.nextInt();

for (count = 20; count >= 0; count--)
{
System.out.println("|");
}
for (count = 1; count <= 40; count++)
{
System.out.print("+");
}
}
}``````

What I'm having trouble with is drawing the diamond. How do I tell the program where to start drawing?

By printing the right number of blank lines before it starts, then printing the right number of spaces before the *... on each line.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.