package mytest;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
System.out.print("請輸入楊輝三角形的行數:");
Scanner scanner = new Scanner(System.in);
int rows = scanner.nextInt();
//設定楊輝三角的行數
//int rows = 10;
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();
package mytest;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
System.out.print("請輸入楊輝三角形的行數:");
Scanner scanner = new Scanner(System.in);
int rows = scanner.nextInt();
//設定楊輝三角的行數
//int rows = 10;
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();
}
}
}