2

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();
    }
}
4

4 回答 4

1

因为你说你想学……这里只是一个例子

对于我们必须添加的每个字母:我们打印单词,然后在单词中间添加新字母。是的,我们必须复制中间的字母。在我的示例中,您通过保存半个字而不是保存整个字来避免这种情况。

string word = "A";

for (int i = 'B'; i < input; i++) {
  print word
  print i
  print inverse word
  print newline

  word = word + i
}

可能这就是你想要的......

编辑:我想添加它是金字塔的空间:只需(input - i)在这些打印之前添加打印空间。

于 2012-07-02T12:43:29.727 回答
1

我将您的程序剥离为正在工作的核心算法:打印树。

所以,这就是我想出的,以及一个演示,这样你就可以看到它的工作原理。

但首先,解释一下:

这使用一个char名为的变量middle来跟踪我们将要打印的当前中间字符。这很有用,因为我们可以创建while循环,它表明我们应该在当前middle字符小于或等于该input字符时继续打印树。

我还计算了我们在第一行左侧需要的空格数spaces。这有点小技巧,所以让我们看看这一行:

int spaces = input - (int) 'A'; // Might be a better way to do w/o cast

我们将空格数归一化,使其范围从 0 到 X,而不是 65 到 (​​X + 65)。因此,对于 , 的输入Aspaces将是0,这是有道理的,因为如果输入只是 ,我们不想打印任何空格A。If inputwas B, spaceswould be 1,因为我们会在第一行打印一个空格。

然后只需设置循环即可:

  1. 打印左侧空格,范围从 0 到spaces
  2. 打印树的左侧,包括中间,范围从Amiddle
  3. 打印树的右侧,范围从middle - 1A
  4. 首先打印具有相同循环的右侧空格(这是不必要的,因为它在控制台中不可见)

最后,我们打印一个换行符,然后需要调整我们的标志。所以我们递减spaces,因为我们刚刚打印了一行输出,并且递增middle,因为我们希望下一行的middle字符是打印的字符之后的下一个字符。

这是注释代码,我从中学到了更好的东西,所以希望我已经解释得足够好。如果您有任何问题,请随时提问。

    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++;
    }  

输入F,这将生成:

     A     
    ABA    
   ABCBA   
  ABCDCBA  
 ABCDEDCBA 
ABCDEFEDCBA
于 2012-07-02T13:13:10.940 回答
0

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.

于 2012-07-02T12:46:36.230 回答
0

好吧,你已经设法建立了金字塔的底部,所以这是一个好的开始。接下来你需要做的是把你的 3 个for循环放在一个更大的for循环中,从那里你'A'到你的输入字符,并在你的小循环中使用它作为结束条件:

  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);
    }
  }

此外,for负责空间的循环可能需要一些额外的工作,目前它不能很好地工作。

于 2012-07-02T12:57:45.180 回答