動態輸出楊輝三角
阿新 • • 發佈:2018-12-30
package javacore;
import java.util.Scanner;
/**
* @author lixw
* @date created in 14:03 2018/12/17
*/
public class YanghuiTriangle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入需要輸出的楊輝三角的行數:");
int rows = in.nextInt ();
for (int i = 0; i < rows; i++) {
int number = 1;
//列印空格字串
System.out.format("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}