Java 构建金字塔并输出第一行

Java 构建金字塔并输出第一行,java,Java,根据下面的代码,我尝试使用如下所示的任何行输入打印出金字塔: * *o* *o*o* 但是有任意数量的行。所有的东西都排好了,但我不知道如何让第一颗星排好。目前,它输出如下内容,行数为5: * *o* *o *o *o* *o *o *o *o *o* *o *o *o *o *o *o *o* *o *o *o *o *o *o *o *o *o* 我的代码: System

根据下面的代码,我尝试使用如下所示的任何行输入打印出金字塔:

      *
     *o*
    *o*o*
但是有任意数量的行。所有的东西都排好了,但我不知道如何让第一颗星排好。目前,它输出如下内容,行数为5:

              *
            *o*
         *o *o *o*
      *o *o *o *o *o*
   *o *o *o *o *o *o *o*
*o *o *o *o *o *o *o *o *o*
我的代码:

System.out.println("Please enter a number of rows: ");
                    int row = scan.nextInt();//takes in number of rows 
                    in=scan.nextLine(); //gets rid of error with .nextInt();                    
                    String s="*";
                    String pat="o";
                    if(row>1){//this will only run through if the number of rows entered is greater than 1

                        for (int i =0; i <= row; i++) {//this for loop is used to build the pyramid pattern
                            // for spacing
                           for (int j = 1; j <= row - i; j++){             
                                System.out.print("   ");      
                            }

                            // left half of the pyramid           
                            for (int k = i; k >= 1; k--){
                                if(k==0){

                                    System.out.println(s);
                                }else{
                                System.out.print((k >= row+1) ? + k: " "+s + pat);
                                }
                            }
                            // corresponding right half of the pyramid
                            for (int k = 2; k <= i; k++) {                                  
                                System.out.print((k >= row+1) ? +k :  " "+ s + pat);
                            }
                            // next line
                            System.out.println("*");
                            }

                    }else{
                        System.out.println(s);
                    }

我会先把问题分成更小更容易处理的部分。首先,忽略扫描仪并输入数据,直到解决方案按您所需工作

制作一个简单的程序,一次打印多个棱锥体,以便您可以在进行更改后快速验证它们是否都能正常工作

我会选择将其拆分为返回字符串的函数。例如,我创建了一个方法“getRow”,它返回一个包含每一行的字符串

创建一行真正需要的是金字塔的高度和当前行索引

通常,通过分解问题,您可以一次专注于一项任务,直到您确定该部分工作正常为止

public static void main(String[] args) {

    printPyramid(1);
    System.out.println("----------------------");
    printPyramid(2);
    System.out.println("----------------------");
    printPyramid(3);
    System.out.println("----------------------");
    printPyramid(4);
    System.out.println("----------------------");
    printPyramid(5);
    System.out.println("----------------------");
}

private static void printPyramid(int height) {
    String pyramid = getPyramid(height);
    System.out.print(pyramid);
}

private static String getPyramid(int height) {
    String pyramid = "";
    for (int i = 1; i <= height; i++) {
        String row = getRow(height, i);
        pyramid += (row + System.lineSeparator());
    }
    return pyramid;
}

private static String getRow(int height, int row) {
    // Put all the pieces together
}

private static String getMargin(int size) {
    // return as many spaces as size
}

private static String getBody(int widthForThisRow) {
    // All bodies are the same, the question is how large they are, use widthForThisRow in a loop and see if the index is divisible by 2.
}

什么是争吵?它是零个或多个空格,后跟a*和n个o*的重复,n=0,1,2,。。。然后前进到下一行

将其转换为Java,暂时忽略空格:

static void printRowVisiblePart(int n) {
  System.out.print("*");
  for (int i = 0; i < n; i++) {
    System.out.print("o*");
  }
  System.out.println();  
}
我们得到:

*
*o*
*o*o*
*o*o*o*
*o*o*o*o*
这很接近。我们需要在每行的开头留出一些空间

有多少?首先写一个小方法来打印它们:

static void printSpaces(int n) {
  for (int i = 0; i < n; i++) {
    System.out.print(' ');
  }
}

您正在将一个空格连接到s和pat+s+pat-您对输出中出现*o感到惊讶吗?@AndyTurner给出了答案。顺便说一句,没有很好的理由来区分金字塔的左右部分:这是完全相同的模式,应该处理的是完全相同的loop@AndyTurner我对这一结果并不感到惊讶。道歉,如果不清楚,但我试图让第一行正确地排在中间。
static void printSpaces(int n) {
  for (int i = 0; i < n; i++) {
    System.out.print(' ');
  }
}
  public static void main(String[] args) {
    int k = Integer.parseInt(args[0]);
    for (int n = 0; n < k; n++) {
      printSpaces(k - 1 - n); // k-1 at n=0 and 0 at n=k-1!
      printRowVisiblePart(n);
    }
  }