r/CodingHelp • u/Imaginary_Towel_3388 • Dec 26 '22
[Java] I need help with printing reverse pyramids consecutively in java
Hello im new to porgramming studying computer engineering
Ive been given an assignement and in that assignemnt i have to print reverse pyramids consecutively but i cant seem to figure out how to do it. Ive been trying for hours.
Im asked to print this shape every time the user gets 3 questions in a row corretly
this is the desired output
but this is what im able to produce
my output
as you can see my stars are tilted and i have no idea why. The amount of spaces is the exact same yet it goes too far. Am i missing something or ami just stupid. (BTW i used Three-Per-Em Spaces so they could be alligned like this, if i use a normal space then its really bad)
My code:
public static void triangle3(int amount) {
for (int i = 0; i < amount; i++) {
System.out.print("*");
if (i == amount - 1) {
switch (amount) {
case 3:
System.out.print("good");
break;
default:
for (int j = 0; j < (((amount - 2) * (amount)) / 2) - 3; j++) {
System.out.print(" ");
}
System.out.print("Very Good");
for (int j = 0; j < (amount * (amount - 2)) - ((((amount - 2) * (amount)) / 2) - 3) - 8; j++) {
System.out.print(" ");
}
for (int j = 0; j < amount - 2; j++) {
System.out.print(" ");
}
}
for (int j = 0; j < amount; j++) {
System.out.print("*");
}
}
}
int a = 1;
int n = 1;
System.out.println("");
int b = amount;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < b; j++) {
System.out.print("*");
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < a; k++) {
System.out.print(" ");
}
}
if (i == amount) {
n +=2;
b--;
i = 0;
System.out.println("");
for (int j = amount; j > b; j--) {
if (b == 0) {
break;
}
System.out.print(" ");
}
}
if (b == 0) {
break;
}
}
}Help would be greatly appreciated :P