二维数组

  • 二维数组树出杨辉三角:打印杨辉三角形(行数可以键盘录入)


    二维数组_第1张图片
 Scanner scanner = new Scanner(System.in);
            System.out.println("请输入行数");
            int row = scanner.nextInt();

            int[][] array = new int[row][];


            for (int i = 0; i < array.length; i++) {
                array[i] = new int[i + 1];
                array[i][0] = 1;
                array[i][i] = 1;
                if (i > 1) {
                    for (int j = 1; j <= i - 1; j++) {
                        array[i][j] = array[i - 1][j] + array[i - 1][j - 1];

                    }
                }

            }
            for (int x = 0; x < array.length; x++) {
                for (int v = 0; v < array[x].length; v++) {
                    System.out.print(array[x][v] + "     ");
                }
                System.out.println();
            }

你可能感兴趣的:(二维数组)