二维数组(杨辉三角)

importjava.util.Arrays;//导入数组包    杨辉三角

publicclass Yhsj

{

       public static void main(String[] args)

       {

              //声明并且构建数组对象

              int[][] arr=new int[9][];

              //for循环确定变量行i的取值范围

              for(int i=0;i<arr.length;i++)

              {

                     //构建新的数组行对象赋值给arr[i]

                     arr[i]=new int[i+1];

                     //for循环确定变量列j的取值范围

                     for(intj=0;j<arr[i].length;j++)

                     {

                            //判断当满足以下情况,1赋值给行列

                            if(i==0||j==0||j==arr[i].length-1)

                            {

                                          arr[i][j]=1;

                            }

                            else

                            {

                                  

                                   arr[i][j]=arr[i-1][j-1]+arr[i-1][j];

                            }

                     }

              }

       //打印的两种方式

              for(int i=0;i<arr.length;i++)

              {

                     for(intj=0;j<arr[i].length;j++)

                     {

                            System.out.print(arr[i][j]+””);

                     }

                     System.out.println();

              }

              for(int i=0;i<arr.length;i++)

              {

                     System.out.println(Arrays.toString(arr[i]));

              }

       }

}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/Albaficahan/article/details/50032151
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞