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

Java 如何以三角形图案打印数字,java,Java,这是它应该打印的图案。这就是我现在做的代码。每个数字之间应有5个空格。有人能帮我吗 1 2 0 2 3 0 0 0 3 模式: 1 2 0 2 3 0 0 0 3 我的代码: 1 2 0 2 3

这是它应该打印的图案。这就是我现在做的代码。每个数字之间应有5个空格。有人能帮我吗

                1
          2     0     2
    3     0     0     0     3
模式:

                1
          2     0     2
    3     0     0     0     3
我的代码:

                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;
}
}
import java.util.Scanner;
公共阶级三角{
公共静态void main(字符串参数[]){
System.out.println(“输入要打印的行数”);
扫描仪a=新的扫描仪(System.in);
int n=a.nextInt();
int[]行=新int[0];
对于(int i=0;i
查找用于打印零的算术序列,其余的应该很简单。

我看到了您的代码,我想我们可以这样做,下面是我修改的代码:

                1
          2     0     2
    3     0     0     0     3
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;
}
公共类三角形{
公共静态void main(字符串参数[]){
System.out.println(“输入要打印的行数”);
扫描仪a=新的扫描仪(System.in);
int n=a.nextInt();
int[]行=新int[0];
对于(int i=0;i
}

                1
          2     0     2
    3     0     0     0     3


这可能就是你想要的答案。

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

                1
          2     0     2
    3     0     0     0     3
1.

                1
          2     0     2
    3     0     0     0     3
     // Padding For Triangle
     System.out.print("      "); // 6 white space
2.

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

                1
          2     0     2
    3     0     0     0     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;
    }
/*查找下一行的值*/
@抑制警告(“空”)
公共静态int[]nextRow(int行[]){
int nextRow[]=null;
if(row.length==0){
nextRow=新整数[1];
nextRow[0]=1;
}否则{
//count++;//提示
nextRow=新整数[row.length+2];
for(int i=0;i

如果你有任何问题,尽管问。祝您好运。

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

                1
          2     0     2
    3     0     0     0     3
Row0, columns 1
Row1, columns 3
Row2, columns 5
Row3, columns 7
也就是说,rowNum*2+1

                1
          2     0     2
    3     0     0     0     3
基于此,我修改了您的代码,下面是可行的解决方案:

                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 = 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;
}
}
5:

                1
          2     0     2
    3     0     0     0     3
10:

                1
          2     0     2
    3     0     0     0     3

所以问题是什么?这还不够清楚。请重新格式化您当前的输出是什么?问题是按照我给出的方式打印图案。我现在打印的内容是:1 2抱歉,我不知道如何在这里打印模式……当你否决投票时,请留下评论,以便我们将来可以改进……只有否决投票然后逃跑是没有意义的,这没有任何改变。