[Java] 纯文本查看 复制代码
import java.util.Scanner;
class YangTest{
public static void main(String[] args){
//创建对象
Scanner sc = new Scanner(System.in);
//设置行数
System.out.println("请输入你想实现杨辉三角的行数:");
int hang = sc.nextInt()+1;
System.out.println(hang);
int[][] number = new int[hang][hang];
number[0][0] = 1;
for(int x=1;x<hang;x++){
number[x][0] = 1;
number[x][x-1] = 1;
}
for(int x=2;x<hang;x++){
for(int y = 1; y<x;y++ ){
number[x][y] = number[x-1][y-1]+number[x-1][y];
}
}
for(int x=0;x<hang;x++){
for(int y=0;y<x;y++){
System.out.print(number[x][y]+"\t");
}
System.out.println();
}
}
} |