import java.util.Scanner;
class YangHui {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("我能输出杨辉三角,请输入行数.....");
int n=sc.nextInt();
print(n);
}
public static void print(int n){
if (n<1){
System.out.println("您需要输入正整数.....");
}else{
int[][] arr=new int[n][n];
for(int x=0;x<arr.length;x++){
for (int y=0;y<=x ;y++ ){
if (y==0 || y ==x){
arr[x][0]=1;
arr[x][y]=1;
}else{
arr[x][y] = arr[x-1][y-1] + arr[x-1][y];
}
}
}
//遍历数组
for (int x=0; x<arr.length; x++){
for (int y= 0;y<=x ;y++ ){
System.out.print(arr[x][y]+"\t");
}
System.out.println();
}
}
}
}
|
|