🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Java 程序:打印 Pascal 三角形 > 原文: [https://beginnersbook.com/2019/02/java-program-to-print-pascal-triangle/](https://beginnersbook.com/2019/02/java-program-to-print-pascal-triangle/) 在本教程中,我们将编写一个 java 程序来打印 [Pascal 三角](https://en.wikipedia.org/wiki/Pascal's_triangle) 。 ## 示例:打印 Pascal 三角 在此程序中,要求用户输入行数,并根据输入,使用输入的行数打印 pascal 三角形。 ```java package com.beginnersbook; import java.util.Scanner; public class JavaExample { static int fact(int num) { int factorial; for(factorial = 1; num > 1; num--){ factorial *= num; } return factorial; } static int ncr(int n,int r) { return fact(n) / ( fact(n-r) * fact(r) ); } public static void main(String args[]){ int rows, i, j; //getting number of rows from user System.out.println("Enter number of rows:"); Scanner scanner = new Scanner(System.in); rows = scanner.nextInt(); scanner.close(); System.out.println("Pascal Triangle:"); for(i = 0; i < rows; i++) { for(j = 0; j < rows-i; j++){ System.out.print(" "); } for(j = 0; j <= i; j++){ System.out.print(" "+ncr(i, j)); } System.out.println(); } } } ``` **输出:** ![Java Program to print Pascal Triangle](https://img.kancloud.cn/d4/a3/d4a3dbb07a34ebbaa444f693f6db6a4e_645x1024.jpg) #### 相关的 Java 示例 1. [Java 程序:找到一个数字的平方根](https://beginnersbook.com/2019/02/java-program-to-find-square-root-of-a-number-without-sqrt/) 2. [Java 程序:检查一个数字是否完美正方](https://beginnersbook.com/2019/02/java-program-to-check-if-given-number-is-perfect-square/) 3. [Java 程序:提取来自输入数字的数字](https://beginnersbook.com/2019/02/java-program-to-break-integer-into-digits/) 4. [Java 程序:使用`switch-case`制作计算器](https://beginnersbook.com/2017/09/java-program-to-make-a-calculator-using-switch-case/)