如何用一维数组实现杨辉三角形输出,能补齐代码么?(语言-java)

    public static void main(String args[]){
       int Line_num;
       int row,col;
       int yh[]=new int[20];     //不超过20行
       Scanner reader=new Scanner(System.in);
       System.out.print("请输入行数:");
       Line_num=reader.nextInt();
       yh[0]=1;
       for (【代码1】){          //控制输出行数为Line_num的循环控制
          for (col=row;col>0;col--)
             yh[col]=yh[col-1]+yh[col];
          for (【代码2】)        //控制输出每行前导空格的循环控制
              System.out.print("     ");
          for (col=0;col<=row;col++)
              【代码3】          //输出下标为col的项的代码
          System.out.println(" " );
       }
    }

代码段1、2、3的补全如下,望采纳

【代码1】应该控制输出行数为Line_num的循环控制。根据题目要求,应把它替换为如下代码:

for (row = 0; row < Line_num; row++)
{
    // 计算每行的值
    for (col = row; col > 0; col--)
    {
        yh[col] = yh[col - 1] + yh[col];
    }
    // 输出每行的值
    for (col = 0; col <= row; col++)
    {
        System.out.printf("%5d", yh[col]);
    }
    System.out.println();
}

【代码2】控制输出每行前导空格的循环控制,可以替换为如下代码:

for (int i = 0; i < Line_num - row - 1; i++)
{
    System.out.print("     ");
}

【代码3】输出下标为col的项的代码,可以替换为如下代码:

System.out.printf("%5d", yh[col]);

望采纳

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        int[] arr = new int[rows];
        arr[0] = 1;
        System.out.println(arr[0]);
        for(int i = 1;i < rows;i++){
            for(int j = i - 1;j > 0;j--){
                arr[j] += arr[j - 1];
            }
            arr[i] = 1;
            for(int k = 0;k <= i;k++){
                System.out.print(arr[k] + " ");
            }
            System.out.println();
        }
    }
}

你看看这个,应该没问题。