Reputation: 35
I am trying to get my program to print a diamond shape with a size defined by the user, but the bottom half is messed up and I'm not sure why. The \'s and /'s must be going down diagonally, with a V at the bottom, making a diamond shape. Can anyone explain what's causing it to mess up?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Boolean f = true;
while (f) {
System.out.print("Choose a diamond size: ");
try {
int diamondSize = Integer.parseInt(input.nextLine());
int g = 0;
while (g < diamondSize +1) {
System.out.print(" ");
g++;
}
System.out.println("^");
for (int i = 1; i <= diamondSize; i++) {
for (int a = 0; a <= diamondSize - i; a++) {
System.out.print(" ");
}
for (int a = 1; a <= 2 * diamondSize - 1; a++) {
if (a == i || a == 0) {
System.out.print("/");
for (int n = 1; n < a * 2; n++) {
System.out.print(" ");
}
System.out.print("\\");
}
}
System.out.println();
}
System.out.print("<");
int e = 0;
while (e <= diamondSize * 2) {
System.out.print(" ");
e++;
}
System.out.println(">");
for (int i = diamondSize * 2; i >= diamondSize; i--) {
for (int a = 1; a >= diamondSize; a--) {
System.out.print(" ");
}
for (int a = 1; a <= 2 * diamondSize - 1; a++) {
if (a == i || a == 0) {
System.out.print("\\");
for (int n = 1; n < a * 2; n++) {
System.out.print(" ");
}
System.out.print("/");
System.out.println();
}
}
}
int m = 0;
while (m < diamondSize + 1) {
System.out.print(" ");
m++;
}
System.out.println("V");
}
catch(Exception e){
System.out.println("RESETTING. Please type an integer this time.");
}
System.out.println("Make another diamond? Type yes or no: ");
if (input.nextLine().equals("no")) {
System.out.print("Thanks for playing!");
System.exit(0);
}
}
}
}
Upvotes: 3
Views: 772
Reputation: 10945
You have two main bugs in your code. The first is that your second outer loop is counting on the wrong index.
When you print the top of the diamond, your outer loop is correct:
for (int i = 1; i <= diamondSize; i++) {
But when you print the bottom half, you are trying to loop over twice the size down to the size for some reason:
for (int i = diamondSize * 2; i >= diamondSize; i--) {
This should be:
for (int i = diamondSize; i >= 1; i--) {
The problem that this causes you is that when you aren't looping over the correct index values, you can't use that loop index in any of your later loops.
Your second error is that your first spaces print loop in the bottom half is trying to count down, but you have the starting value and condition reversed, and you're counting against a hardcoded 1
instead of the outer loop index of i
.
This:
for (int a = 1; a >= diamondSize; a--) {
Should be this:
for (int a = diamondSize; a >= i; a--) {
Replace these two lines of your code:
for (int i = diamondSize * 2; i >= diamondSize; i--) {
for (int a = 1; a >= diamondSize; a--) {
With this:
for (int i = diamondSize; i >= 1; i--) {
for (int a = diamondSize; a >= i; a--) {
As a stylistic issue, your code is extremely hard to read and debug because of unnecessary complexity. You should really consider refactoring some of the common work (such as printing spaces) out into their own methods. I was unable to debug your code until I had substantially re-written it.
For instance, you can refactor out all the space-prints loops by writing a method like this:
private static void printSpaces(int spaces) {
for (int i = 0; i < spaces; i++) {
System.out.print(" ");
}
}
That would let you replace code like this:
int g = 0;
while (g < diamondSize +1) {
System.out.print(" ");
g++;
}
with a call to that method like this:
printSpaces(diamondSize + 1);
This doesn't change the behavior of your program, but it makes it easier for yourself and others to read it, and see what it's doing.
Upvotes: 2
Reputation: 13596
The problem is here:
for (int a = 1; a <= 2 * diamondSize - 1; a++) {
if (a == i || a == 0) {
System.out.print("\\");
for (int n = 1; n < a * 2; n++) {
System.out.print(" ");
}
System.out.print("/");
System.out.println();
}
}
You are adding spaces after the "\" and before the "/". However, you need to place hald the spaces before the "\" and half after.
I've tried to bugfix it but I can't really make head or tail out of your many nested blocks and stuff, but I'm sure you can figure this one out. Sorry about that.
Upvotes: 1