Home >Java >javaTutorial >How to use java to print a 10-line Yang Hui triangle using a two-dimensional array

How to use java to print a 10-line Yang Hui triangle using a two-dimensional array

WBOY
WBOYforward
2023-05-17 10:22:051483browse

Use a two-dimensional array to print a 10-line Yang Hui triangle

public class T02 {
    public static void main(String[] args) {
        //声明并且初始化数组
        int[][]arr=new int[10][];
        //给数组的元素赋值
        for(int i=0;i< arr.length;i++){
            arr[i]=new int[i+1];
            arr[i][0]=arr[i][i]=1;
            if(i>1){
                for(int k=1;k<arr[i].length-1;k++){
                    arr[i][k]=arr[i-1][k-1]+arr[i-1][k];
                }
            }
        }
        //遍历数组
        for(int i=0;i< arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

The above is the detailed content of How to use java to print a 10-line Yang Hui triangle using a two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete