[Java]打印杨辉三角,对于维数可用户自定义
来源:互联网 发布:js鼠标悬停事件实例 编辑:程序博客网 时间:2024/05/16 08:46
- import java.util.Scanner;
- class Quiz1 {
- public static void main(String[] args) {
-
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入你所需的杨辉三角的维数:");
- int n = sc.nextInt();
- int[] [] array = new int [n] [];
-
-
- for (int i = 0; i < array.length; i++) {
- array[i] = new int [i + 1];
- }
-
-
- array[0] [0] = 1;
- array[1] [0] = 1;
- array[1] [1] = 1;
- for (int i = 2; i < array.length; i++) {
- array[i] [0] = 1;
- array[i] [i] = 1;
- for (int j = 1;j < i; j++) {
- array[i] [j] = array[i - 1] [j - 1] + array[i - 1] [j];
- }
- }
-
-
- for (int i = 0; i < array.length;i++){
- for (int j = 0; j < array[i].length ; j++) {
- System.out.print(array[i] [j] + "/t");
- }
- System.out.println();
- }
- }
- }