Code written in java
I'm trying to print the user's input amount of "*" on the same line.
Here is what I have:
if (((input / 2) + 1) == ir) {
for (int ij = 1 ; ij <= input; ij++) {
System.out.print("*");
}
}
the if statement is testing to see if we are at the halfway point of the shape I'm trying to make (a bowtie).
I feel like my logic and code is correct but for an input of 5,
this particular line looks like this: ***
instead of: *****
Can someone explain to me why this is happening?
Here is the full code:
import java.util.Scanner;
public class BowTie {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
for (int ic = 1; ic <= spaces; ic++) {
if (((input / 2) + 1) == ir) {
for (int ij = 1; ij <= input; ij++) {
System.out.print("*");
}
} else {
System.out.print(" ");
}
}
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
} else {
return;
}
scnr.close();
}
}