下面是一個用Java輸出等腰楊輝三角的示例代碼:
import java.util.Scanner;
public class YangHuiTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入楊輝三角的行數:");
int numRows = scanner.nextInt();
int[][] triangle = new int[numRows][];
for (int i = 0; i < numRows; i++) {
triangle[i] = new int[i + 1];
triangle[i][0] = 1;
triangle[i][i] = 1;
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numRows - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(triangle[i][j] + " ");
}
System.out.println();
}
}
}
運行這個代碼,輸入要輸出的楊輝三角的行數,程序會輸出相應行數的等腰楊輝三角形狀。