1. 程式人生 > >動態輸出楊輝三角

動態輸出楊輝三角

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(); } } }

在這裡插入圖片描述

在這裡插入圖片描述