【问题标题】:Java - How to create alternating triangle pyramid?Java - 如何创建交替三角金字塔?
【发布时间】:2016-09-24 00:48:38
【问题描述】:

我正在尝试创建一个交替的“*”和“o”字符的三角金字塔,行数基于用户输入。如果用户输入“6”作为行数,我试图实现的预期输出是:

      *
     *o*
    *o*o*
   *o*o*o*
  *o*o*o*o*
 *o*o*o*o*o*

我为此编写的代码是:

String star = "*";
String circle = "o";
System.out.println("Please enter number of rows: ");
int rows = in.nextInt();
for (int i = 0; i < rows; i++){
    for (int j = 0; j < rows-i; j++){
        System.out.print(star);
    }
    for (int k = 0; k <= i; k++){
        System.out.print(circle);
    }
    System.out.println();
}

但是,我的代码输出与上面的金字塔不匹配。我的代码的输出,用户输入为“6”,是:

******o
*****oo
****ooo
***oooo
**ooooo
*oooooo

在过去三个小时搜索了这个网站和其他网站之后,我仍然对如何交替字符、如何在每行中有正确数量的字符以及如何按预期格式化金字塔感到迷茫输出是。我不知道我的代码是否完全错误,或者我是否只是遗漏了一部分以使其正常工作,但非常感谢任何建议或参考。

【问题讨论】:

标签: java


【解决方案1】:

你可以用另一种更简单的方法来处理它。

在伪代码中:

  • 创建一个由n 空格组成的字符串
  • 添加"*"
  • 循环n 次,循环的每次迭代:
    • 打印出来
    • " *" 替换为"*O*"

这识别出一种创建第一行的简单方法,以及一种从上一行创建下一行的简单方法。每个替换将仅匹配最后一个(前导)空格和第一个星,用星替换空格,用 O 替换星并添加星。

通常,解决难题的最佳方法是以使其成为简单问题(或简单问题的集合)的方式来看待它。


创建n 空格字符串的几种方法:

  • 每次迭代都添加' ' 的循环
  • new String(new char[n]).replace('\0', ' ')

如何用其他字符替换字符串中的某些字符:

str = str.replace(" *", "*O*");

【讨论】:

    【解决方案2】:

    这个方法可以正常工作:

    public void printPyramid (int input) {
        for (int row = 1; row <= input; row++) {
            for (int whitespace = input - 1; whitespace >= row; whitespace--) {
                System.out.print(" ");
            }
            System.out.print("*");
            for (int circle = 1; circle < row; circle++) {
                System.out.print("o*");
            }
            System.out.println();
        }
    }
    
                             *
                            *o*
                           *o*o*
                          *o*o*o*
                         *o*o*o*o*
                        *o*o*o*o*o*
                       *o*o*o*o*o*o*
                      *o*o*o*o*o*o*o*
                     *o*o*o*o*o*o*o*o*
                    *o*o*o*o*o*o*o*o*o*
                   *o*o*o*o*o*o*o*o*o*o*
                  *o*o*o*o*o*o*o*o*o*o*o*
                 *o*o*o*o*o*o*o*o*o*o*o*o*
                *o*o*o*o*o*o*o*o*o*o*o*o*o*
               *o*o*o*o*o*o*o*o*o*o*o*o*o*o*
              *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
             *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
            *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
           *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
          *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
         *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
    

    【讨论】:

    • 感谢大家的回复!他们肯定很有帮助,给了我一些新的思考! Marcel 的回答对我来说是最容易理解的,而且效果很好!
    【解决方案3】:

    欢迎来到堆栈溢出! 首先,“o”和“*”不是交替的,因为 for 循环一直执行到完成。这意味着星星和圆圈将分别打印出来。对于这个应用程序,您只需要一个 for 循环和两个基于 for 循环中的“i”是奇数还是偶数的 if 语句。一个简单的方法是使用模函数:

    String star = "*";
    String circle = "o";
    System.out.println("Please enter number of rows: ");
    int rows = in.nextInt();
    for (int i = 0; i < rows; i++){
        if ((i % 2) == 0)
        {
            System.out.print(circle);
        }
        else
        {
            system.out.print(star);
        }
     System.out.println();
    }
    

    看看这是否有效。 谢谢!

    【讨论】:

      【解决方案4】:

      这里有一个解决方案,简单易懂,对初学者友好。
      (如果您想更高级,请查看@Bohemian♦ 的解决方案)

      String star = "*";
      String circle = "o";
      System.out.println("Please enter number of rows: ");
      int rows = in.nextInt();
      for (int i = 0; i < rows; i++){
          // How many "o" do we need?
          int count_circle = i;
      
          // How many * do we need?
          int count_star = count_circle + 1;
      
          // Let's create the string with o and *
          String output = "";
          for(int j = 0; j < (count_circle + count_star); j++){
              if(j % 2 == 0) // if it is odd
                  output = output + star;
              else // if it is even
                  output = output + circle;
          }
      
          // Do we need spaces at the beginning?
          String spaces = "";
          for(int j = 0; j < rows - i - 1; j++){
              spaces = spaces + " ";
          }
      
          // Final output
          output = spaces + output;
          System.out.println(output);
      }
      

      【讨论】:

        【解决方案5】:

        试试这个。

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number of rows: ");
        int rows = in.nextInt();
        for (int i = 0; i < rows; ++i) {
            System.out.printf("%" + (rows - i) + "s", "*");
            for (int j = 0; j < i; ++j)
                System.out.print("o*");
            System.out.println();
        }
        

        【讨论】:

          【解决方案6】:

          例如: 如果 rows=3

          *##

          **#

          ***

          class Main{
          public static void main(String x[]){
              Scanner scan=new Scanner(System.in);
              int rows=scan.nextInt();
              for(int i=1;i<rows;i++){
                  for (int j=0;j<i;j++)
                  {
                  System.out.print("*");
                  }
              for (int j=rows;j>i;j--)
                  {
                  System.out.print("#");
                  }
              System.out.println();
              }
          }
          }
          

          看看这是否有效。 谢谢!

          【讨论】:

          • 请添加一些关于此解决方案如何工作的说明。
          猜你喜欢
          • 2018-02-17
          • 1970-01-01
          • 1970-01-01
          • 2020-02-11
          • 1970-01-01
          • 2021-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多