打印空间以根据我的金字塔图案对齐数字

时间:2016-10-17 00:40:06

标签: java algorithm

听起来比看起来容易得多。基本上我有我的代码完成这是我的输出,其中前导数字是程序接收的任何整数作为输入。在这种情况下,n = 5:

        1
      2 1
    3 2 1
  4 3 2 1
5 4 3 2 1

但这就是它的样子:

    1
   2 1
  3 2 1
 4 3 2 1
5 4 3 2 1

在保持这种模式的同时,我应该如何在我的数字之间添加空格?我已经尝试过在这里和那里进行编辑,但它仍然会像这样出现:

import java.util.Scanner;

public class DisplayPattern {
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer and I will display a pattern for you: ");
      int n = input.nextInt();
      displayPattern(n);
   }

   public static void displayPattern(int n) {
      final int MAX_ROWS = n;
      for (int row = 1; row <= MAX_ROWS; row++) { 
         for (int space = (n - 1); space >= row; space--) {
            System.out.print(" ");
         }

         for (int number = row; number >= 1; number--) {
            System.out.print(number + " "); /*<--- Here is the edit.*/
         }
         System.out.println();
      }
   }

我的代码:

package main


type StringA string


func main() {

    var s StringA
    s = "hello"
    s0 := s.(StringB) <---- somehow cast my StringA to StringB. After all, they are both strings
    s0.Greetings()

}

编辑: @weston让我在第二次尝试时显示我的代码。这真的不是一个很大的改变。我所做的只是在数字的打印声明后添加一个空格。我将编辑上面的代码以反映这一点。由于它似乎可能更接近我的结果,我将从那里开始并继续绞尽脑汁。

5 个答案:

答案 0 :(得分:0)

我设法使该程序正常工作,但这仅适用于单个数字(即最多9个)。

import java.util.Scanner;

public class Play
{
    public static class DisplayPattern
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);

            System.out.print("Enter an integer and I will display a pattern for you: ");
            int n = input.nextInt();

            displayPattern(n);
        }

        public static void displayPattern(int n)
        {
            final int MAX_ROWS = n;
            final int MAX_COLUMNS = n + (n-1);
            String output = "";

            for (int row = 1; row <= MAX_ROWS; row++)
            {
                // Reset string for next row printing
                output = "";

                for (int space = MAX_COLUMNS; space > (row+1); space--) {
                    output = output + " ";
                }

                for (int number = row; number >= 1; number--) {
                    output = output + " " + number;
                }

                // Prints up to n (ignore trailing spaces)
                output = output.substring(output.length() - MAX_COLUMNS);
                System.out.println(output);
            }
        }
    }
}

答案 1 :(得分:0)

适用于所有n

i行打印(n-1 - i) * length(n)个空格中,然后打印i+1个数字,以1结尾,用length(n)个空格分隔。

public static void printPiramide(int n) {
    int N = String.valueOf(n).length();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            for (int k = 0; k < N; k++)
                System.out.print(" ");
        }
        for (int j = i+1; j > 0; j--) {
            int M = String.valueOf(j).length();
            for (int k = 0; k < (N - M)/2; k++) {
                System.out.print(" ");
            }
            System.out.print(j);
            for (int k = (N - M)/2; k < N +1; k++) {
                System.out.print(" ");
            }

        }
        System.out.println();
    }
}

答案 2 :(得分:0)

1:1am

适用于任意数量的行(n)

答案 3 :(得分:0)

解决问题的方法:

IntStream.rangeClosed(1, MAX)
            .forEach(i -> IntStream.range(0, MAX)
                    .map(j -> MAX - j)
                    .mapToObj(k -> k == 1 ? k + "\n" : k <= i ? k + " " : "  ")
                    .forEach(System.out::print)
            );

MAX = 5的输出:

        1
      2 1
    3 2 1
  4 3 2 1
5 4 3 2 1

答案 4 :(得分:-1)

对于底行,您有正确的空格数。但对于从底部开始的下一行,您在左侧缺少一个空格(4个空格超出1个空格)。在下一行中,您在左侧缺少两个空格(3个在2个空格之外)......依此类推。

您在每行的开头添加了一些空格,但您只考虑了您要打印的位数。但是,您还需要考虑在前一行中打印的空格数。

一旦你使这个部分工作,你也可以考虑当你开始达到两位数的数字时会发生什么,以及它会如何影响空间的数量。你真正想做的是在左边填充字符串,使它们的长度与最长的行相同。您可以查看String.format()方法来执行此操作。