import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
System.out.print("Please input your rows:");
int row=scan.nextInt();
scan.close();
calculate(row);
}
public static void calculate(int row) {
for(int i=1;i<=row;i++) {
for(int j=1;j<=row-i;j++) {
System.out.print(" ");
}
for(int j=1;j<=i;j++) {
System.out.print(num(i,j)+" ");
}
System.out.println();
}
}
public static int num(int x,int y) {
if(y==1||y==x) {
return 1;
}
int c=num(x-1,y-1)+num(x-1,y);
return c;
}
}
杨辉三角Java语言实现
最新推荐文章于 2021-02-27 12:52:47 发布