Java - 如何创建交替的三角形金字塔?

4

我正在尝试创建一个由交替的“*”和“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

在过去的三个小时里,我搜索了这个网站和其他网站,但是我仍然不知道如何交替字符、每行有正确数量的字符以及如何格式化金字塔使其符合预期输出。我不知道我的代码是否完全错误,或者我只是缺少一部分以使其正常工作,但是任何建议或参考资料都将不胜感激。


你已经查看了使用for循环创建三角形吗? - Mick Mnemonic
6个回答

5
您可以采用一种更简单的方法来解决这个问题。
伪代码如下:
创建一个有n个空格的字符串
将*添加到它后面
循环n次,每次迭代:
打印出它
使用“* O *”替换“*”
这种方法能够简单地创建第一行和之后的每一行。每次替换只匹配最后(领先)的空格和第一个星号,将空格替换为星号,将星号替换为O并添加星号。
通常,解决难题的最佳方法是从使其成为简单问题(或一系列简单问题)的角度来看待它。
以下是创建n个空格字符串的几种方法:
每次迭代添加' '的循环
new String(new char[n]).replace('\0', ' ')
如何用其他字符替换String中的特定字符:
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的答案最容易让我理解,并且它非常有效! - ccbadger

1

欢迎来到Stack Overflow! 首先,"o"和"*"不是交替出现的,因为for循环会执行到结束。这意味着星号和圆圈将分别打印出来。对于这个应用程序,您只需要一个for循环和两个if语句,根据for循环中的"i"是奇数还是偶数。使用取模函数是一种简单的方法:

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

看看这是否有效。谢谢!

1
这是一个易于理解且适合初学者的解决方案。
(如果您想更进一步,可以查看@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);
}

1
尝试这个。
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();
}

0

例如:如果行数为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();
    }
}
}

看看是否有效。 谢谢!


请在这个解决方案上添加一些说明,说明它是如何工作的。 - Harshal Parekh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接