import java.util.Scanner;
public class Yang_Hui_Triangle {
public static void main(String[] args) {
System.out.println("请输入您要打印杨辉三角的行数:");
Scanner myScanner = new Scanner(System.in);
int N = myScanner.nextInt();
int [][] yangHui = new int[N][];
System.out.println("yangHui.length = " + yangHui.length);
for(int i = 0; i < yangHui.length; i++){
yangHui[i] = new int[i + 1];
// System.out.println("yangHui["+ i +"].length = " + yangHui[i].length);
for(int j = 0; j < yangHui[i].length; j++){
if((j == 0) || (j == i) || i <= 1){
yangHui[i][j] = 1;
}else if(i > 1){
yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
}
// System.out.print(yangHui[i][j] + "\t");
}
}
for(int k = 0;k < N; k++){
System.out.print("==\t");
}
System.out.println();
for(int i = 0; i < yangHui.length; i++){
for(int j = 0; j < yangHui[i].length; j++){
System.out.print(yangHui[i][j] + "\t");
}
System.out.println();
}
for(int k = 0;k < N; k++){
System.out.print("==\t");
}
System.out.println();
}
}
Java_用数组保存并显示杨辉三角
最新推荐文章于 2021-04-14 21:34:43 发布