Reputation: 73
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();
}
}
Upvotes: 2
Views: 9795
Reputation: 744
Because you said you want to learn sth... here's just an example
For each letter we have to add: we print the word and then add the new letter in the middle of the word. And yes we have to duplicate the letter in the middle. In my example you avoid this by saving the half word instead of saving the whole word.
string word = "A";
for (int i = 'B'; i < input; i++) {
print word
print i
print inverse word
print newline
word = word + i
}
probably that is what you wanted...
edit: I frogot to add the space that it's a pyramid: just add print (input - i)
spaces before those prints.
Upvotes: 1
Reputation: 59699
I stripped down your program to the core algorithm that is at work: Printing the tree.
So, here is what I came up with, along with a demo so you can see it working.
But first, an explanation:
This uses a char
variable named middle
to keep track of what the current middle character is that we are about to print. This is useful, as we can create the while
loop, which states that we should continue to print the tree while the current middle
character is less than or equal to the input
character.
I also calculate the number of spaces we'll need on the left hand side of the first row with spaces
. This is a bit of a trick, so lets look at this line:
int spaces = input - (int) 'A'; // Might be a better way to do w/o cast
We normalize the number of spaces so it ranges from 0 to X, instead of 65 to (X + 65). So, for an input of A
, spaces
will be 0
, which makes sense, since we wouldn't want to print any spaces if the input was just A
. If input
was B
, spaces
would be 1
, since we'd print one space on the first line.
Then it's just a matter of setting up the loops:
spaces
A
to middle
middle - 1
to A
Finally, we print a newline and then need to adjust our flags. So we decrement spaces
, since we just printed a line of output, and increment middle
, since we want the next line's middle
character to be the next character after the one that printed.
Here is the commented code, which I learn better from seeing, so hopefully I've explained it well enough. If you have questions, feel free to ask.
char middle = 'A'; int spaces = input - (int) 'A';
while( middle <= input) {
// Print the left side spaces
for( int i = 0; i < spaces; i++)
System.out.print(" ");
// Print the left side of the tree (including the middle)
for( char y = 'A'; y <= middle; y++)
System.out.print( y);
// Print the right side of the tree
for( char y = Character.toChars( middle - 1)[0]; y >= 'A'; y--)
System.out.print( y);
// Print the right side spaces
for (int i = 0; i < spaces; i++)
System.out.print(" ");
// Print a new line
System.out.println();
// Subtract 1 from the number of spaces we need
spaces--;
// Increment the middle character
middle++;
}
With an input of F
, this will generate:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
Upvotes: 1
Reputation: 46219
Well, you have managed to build the bottom of the pyramid, so that's a good start. What you need to do next is to put your 3 for
loops in a bigger for
loop, where you go from 'A'
to your input character, and use this as end condition in your smaller loops:
for(...) {
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);
}
}
Also, the for
loop responsible for spaces probably needs some extra work, it doesn't work very well at the moment.
Upvotes: 0
Reputation: 1749
You're really close to what you want. You need to use a loop to adjust your starting point and output from A, then B, C, D, E, F etc.
if ( ! ( Character.isAlphabetic( input ) ) ) {
System.out.println( "Error: Invalid letter: " + input );
} else {
for ( char n = 'A'; n <= input; n++ ) {
pyramid( n );
}
}
I tried this and it worked for me. I didn't change any code in your loops, just extracted it to a method.
Upvotes: 0