- package com.test1;
- import java.io.*;
- public class model1 {
- /**
- * @param args
- * 楊輝三角
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- System.out.print("請輸入需要顯示的行數:");
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- String s = null;
- s = br.readLine();
- int n = Integer.parseInt(s);
- Demo1 d = new Demo1();
- d.test1(n);
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
- class Demo1 {
- public void test1(int n) {
- int i, j;
- int m;
- int[][] arry;
- arry = new int[n][n];
- for (i = 0; i < n; i++) {
- for (m = 0; m < n - i; m++) {
- System.out.print(" ");
- }
- for (j = 0; j < n; j++) {
- if (j == 0 || j == i) {
- System.out.print("1");
- System.out.print(" ");
- arry[i][j] = 1;
- } else {
- if (j < i) {
- arry[i][j] = arry[i - 1][j - 1] + arry[i - 1][j];
- System.out.print(arry[i][j]);
- if (arry[i][j] > 10) {
- System.out.print(" ");
- } else {
- System.out.print(" ");
- }
- } else {
- System.out.print(" ");
- }
- }
- }
- System.out.println();
- }
- }
- }