编程输出杨辉三角的前10行---多维数组的应用---java实现
程序员文章站
2022-07-11 08:37:21
import java.util.Scanner;public class yanghui{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("\nPlease ......
import java.util.scanner;
public class yanghui
{
public static void main(string[] args){
scanner sc=new scanner(system.in);
system.out.println("\nplease enter the number of yang hui triangle rows:");
int n=sc.nextint();
int [][]a=new int [n][];
for(int i=0;i<n;i++){
a[i]=new int[i+1];//determine the length of each one-dimensional array.
for(int j=0;j<=i;j++){
if(j==0)
a[i][j]=1;
if(i==j)
a[i][j]=1;
}}
for(int i=2;i<n;i++){
for(int j=1;j<i;j++){
a[i][j]=a[i-1][j]+a[i-1][j-1];
}}
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
system.out.printf("\t"+a[i][j]);
}
system.out.println();
}
}}