用Java学写了一个“打印输出杨辉三角的函数”,请批评

来源:互联网 发布:网络黑彩公司怎么赚钱 编辑:程序博客网 时间:2024/05/05 08:19

public class Yanghuisj{
 
 public static void main(String args []){
  yhsj(15);//调用打印函数
 }  
 
 public static void yhsj(int n){
  int [][] a = new int [n][n];//定义二维数组
  for(int i = 0; i < n; i++){
   a[i][0] = 1;//设置二维数组定值
   a[i][i] = 1;
   for(int j = 0; j < i; j++){
    if(i > 1 && j >= 1){
     a[i][j] = a[i-1][j-1] + a[i-1][j];//计算数值
    }
   }
  }
  for(int i = 0; i < n; i++){
   for(int j = n - i; j > 1; j--){
    System.out.print(" ");//输出每行前面的空格
   }
   for(int j = 0; j < i+1; j++){
    System.out.print(a[i][j] + " ");//输出各行数值
   }
   System.out.println();
  }
 }
}