I'm having trouble with this homework assignment and would like some assistance. I do not want just solutions, as I want to learn from them.
We are doing a letter pyramid using loops. I can't seem to figure out how to put these FOR loops together to make this work as instructed.
The user should enter a letter (or other character which gives an error) then the program converts the letter to upper case if not already. After the conversion to upper case, loops are used in order to go from the character 'A' to whatever the user enters then back to 'A' again. An example is below.
I have attached my code that I have come up with but the output of the program should be as below but the lines are supposed to be spaces. I just added them for spacing:
Enter a single letter (Enter alphabet to display Pyramid): E
____A
___ABA
__ABCBA
_ABCDCBA
ABCDEDCBA
My output is simply this:
Please enter a single letter:
f
ABCDEFEDCBA
Here is the code:
import java.util.*; // For using Scanner class for input
public class LetterPyramid {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.println("Please enter a single letter:");
char input = key.next().charAt(0);
input = Character.toUpperCase(input);
if (!(Character.isLetter(input))) {
System.out.println("Error: Invalid letter: " + input);
} else {
for (int i = input; i < 'Z'; i++) {
System.out.print(" ");
}
}
for (char y = 'A'; y < input; y++) {
System.out.print(y);
if (y == input) {
System.out.println(y);
}
}
for (char x = input; x >= 'A'; x--) {
System.out.print(x);
}
System.out.println();
}
}