I know it's been asked but I'm having some hard time understanding other programs and nested loops, if someone has a trick to follow nested loops and making a shape with asterisks I would like to know because it really got me confused.
Here is the code I wrote after many many tries and experiments:
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int userentry = input.nextInt();
for (int i = 0; i < userentry - 1; i++) {
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
for (int j = userentry - 1; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < userentry; i++) {
for (int k = 0; k < i + 1; k++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
The output for the number entry 3 for example goes like this, i'm having trouble with the spacing:
***
**
*
**
***
I would really appreciate someone who can help me with my program and explain me the algorithm and how can I make more patterns.
Because each character in the font used for input/output/code all have the same width, you have to decrease each iteration by 2, like:
or
instead of one. For spacing, use the following: