建立金字塔并输出第一行

时间:2015-09-29 23:22:17

标签: 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.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);
                    }

2 个答案:

答案 0 :(得分:0)

我首先将问题分解为更小的更易处理的部分。首先,忽略扫描仪和输入数据,直到解决方案按照您的意愿工作。

制作一个可以同时打印多个金字塔的简单程序,这样您就可以在进行更改后快速验证它们是否全部有效。

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

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

通常,通过拆分问题,您可以一次关注一个任务,直到您确定该部分正在运行。

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.
}

答案 1 :(得分:0)

什么是排?它是零个或多个空格后跟*后跟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();  
}

为了好玩,让我们用它来打印出高度k金字塔的可见部分:

public class PyramidPrinter {

  public static void main(String[] args) {
    int k = Integer.parseInt(args[0]);
    for (int n = 0; n < k; n++) {
      printRowVisiblePart(n);
    }
  }

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

有更好的方法,但这很好。

现在向后工作。对于最后一行,我们n = k-1从用户处读取k。我们知道在这一行中我们需要零空间。检查你的照片,注意每行比其后续行还有一个空间。对于第0行,我们需要k-1个空格,对每个连续的行倒计时,所以我们在第k-1行得到零。在行n中,我们需要k - 1 - n个空格。因此,稍微更改main以在行的每个可见部分之前打印这些空格。

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