如何以三角形图案打印数字

时间:2013-11-22 00:57:24

标签: java

这是它应该打印的模式。这就是我现在所做的代码。每个数字之间应该有5个空格。有谁可以帮助我吗?

模式:

                1
          2     0     2
    3     0     0     0     3

我的代码:

import java.util.Scanner;

public class Triangle{

public static void main(String args[]) {

    System.out.println("Input the number of lines you want to print.");
     Scanner a = new Scanner(System.in);
     int n = a.nextInt();
     int[] row = new int[0];
     for(int i=0 ; i < n ; i++){
         row = nextRow(row);
          for(int j=0;j < n-i;j++){

            //Padding For Triangle
            System.out.print(" ");

        }
        //Output the values
      for(int j=0 ; j < row.length ; j++){

            System.out.print(row[j]+" ");

        }
        //Start New Line
        System.out.println();
    }
}
/*Find Values Of Next Row*/
public static int[] nextRow(int row[]){

    int nextRow[] = new int [row.length+1];

    nextRow[0] = row.length+1;               
    nextRow[nextRow.length-1] =row.length+1;
    for(int i=1 ; i < nextRow.length-1 ; i++){

        nextRow[i] = 0;
    }
    return nextRow;
}
}

4 个答案:

答案 0 :(得分:0)

这是一些修改后的代码。它不完整,仍然需要更新您的代码以获得所需的结果。那部分我要留给你。我还在代码中留下了提示,以便您可以修改。

1.

     // Padding For Triangle
     System.out.print("      "); // 6 white space

2.

     System.out.print(row[k]);
     System.out.print("     "); // 5 white space

3.

    /* Find Values Of Next Row */
    @SuppressWarnings("null")
    public static int[] nextRow(int row[]) {
        int nextRow[] = null;
        if (row.length == 0) {
            nextRow = new int[1];
            nextRow[0] = 1;
        } else {
            // count++; // Hint
            nextRow = new int[row.length + 2];

            for (int i = 0; i < nextRow.length; i++) {
                if ((i == 0 || i == nextRow.length - 1)) {

                    nextRow[i] = nextRow.length - 2;
                    // nextRow[i] = count;

                } else {
                    nextRow[i] = 0;

                }
            }
        }
        return nextRow;
    }

如果您有任何疑问,请询问。祝你好运。

答案 1 :(得分:0)

首先必须确定输出所需的模式,在您的情况下是:

Row0, columns 1
Row1, columns 3
Row2, columns 5
Row3, columns 7

,即rowNum * 2 + 1

基于此,我修改了你的代码,这是工作解决方案:

import java.util.Scanner;

public class Triangle{
public static void main(String args[]) {
    System.out.println("Input the number of lines you want to print.");
    Scanner a = new Scanner(System.in);
    int n = a.nextInt();
    int[] row = null;
    for(int i=0 ; i < n ; i++){
        row = nextRow(i);
        for(int j=0;j < n-i;j++){
            //Padding For Triangle
            System.out.print("  ");
        }
        //Output the values
        for(int j=0 ; j < row.length ; j++){
            System.out.print(row[j]+" ");
        }
        //Start New Line
        System.out.println();
    }
}
/*Find Values Of Next Row*/
public static int[] nextRow(int rowNum){
    int nextRow[] = new int [rowNum*2+1];

    nextRow[0] = rowNum+1;//-rowNum/2;
    nextRow[nextRow.length-1] = nextRow[0];
    for(int i=1 ; i < nextRow.length-1 ; i++){
        nextRow[i] = 0;
    }
    return nextRow;
}
}

这是一些输出:

2:

    1
  2 0 2

5:

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

10:

                   1 
                 2 0 2 
               3 0 0 0 3 
             4 0 0 0 0 0 4 
           5 0 0 0 0 0 0 0 5 
         6 0 0 0 0 0 0 0 0 0 6 
       7 0 0 0 0 0 0 0 0 0 0 0 7 
     8 0 0 0 0 0 0 0 0 0 0 0 0 0 8 
   9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10

答案 2 :(得分:-1)

我看到了你的代码,我认为我们可以这样做,以下是我修改过的代码:

public class Triangle {

public static void main(String args[]) {

    System.out.println("Input the number of lines you want to print.");
    Scanner a = new Scanner(System.in);
    int n = a.nextInt();
    int [] row = new int[0];
    for (int i = 0; i < n; i++) {
        row = nextRow(row);
        for (int j = 0; j < n - i; j++) {

            // Padding For Triangle
            System.out.print("   ");

        }
        // Output the values
        for (int j = 0; j < row.length; j++) {

            System.out.print(row[j] + "     ");

        }
        // Start New Line
        System.out.println();
    }
}

/* set space between each other. */
public static String printSpace(int n) {
    String result = "";
    for (int i = 0; i < n; i++) {
        result += "     ";// 5 space
    }

    return result;
}

/* Find Values Of Next Row */
public static int [] nextRow(int row[]) {

    int nextRow[] = new int[row.length + 1];

    nextRow[0] = row.length + 1;
    nextRow[nextRow.length - 1] = row.length + 1;
    for (int i = 1; i < nextRow.length - 1; i++) {

        nextRow[i] = 0;
    }
    return nextRow;
}

}


可能是你想要的答案。

答案 3 :(得分:-2)

查找用于打印出零的算术序列,其余的应该是微不足道的。