Java write a program to read an integer,n,from the keyboard and output n row of pascal's triangle.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/22 12:35:01
Java write a program to read an integer,n,from the keyboard and output n row of pascal's triangle.
Java write a program to read an integer,n,from the keyboard and output n row of pascal's triangle.
Java write a program to read an integer,n,from the keyboard and output n row of pascal's triangle.
OK. I'll see it today~
import java.util.Scanner;
public class PascalTriangle {
public static void main(String[] args) {
System.out.print("Please input a digit for pascal triangle: ");
Scanner scanner = new Scanner(System.in);
final int num = scanner.nextInt();
int[][] ary = getPascalTriangle(num);
for(int[] item: ary){
for(int value: item){
System.out.print((value > 0? value: "") + " ");
}
System.out.println();
}
}
private static int[][] getPascalTriangle(final int num) {
int[][] ary = new int[num][num];
for(int i = 0; i < ary.length; i++){
ary[i][0] = 1;
ary[i][i] = 1;
}
for(int i = 1; i < ary.length; i++){
for(int j = 1; j