Java杨辉三角(方法的调用)

import java.io.*;

public class YangHuiSanJiao {
public static int fac(int n){
if(n==0||n==1)
return n=1;
else
return n*fac(n-1);
} //递归法求阶乘

public static int yanghui(int a,int b){
return fac(a)/(fac(a-b)*fac(b));
} //计算杨辉三角没个数字的规律

public static void main(String[] args){
String line=""; //赋初值有必要,并不保证下面第28行赋值成功,
int hangshu;

while(1==1){
System.out.print("Please enter the lines:");
try{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
line=in.readLine();
}catch(IOException e){} //捕获异常代码

hangshu=Integer.parseInt(line);

if(hangshu>0) //与整个while语句体判断输入是否大于零
break;
}

for(int i=0;i<hangshu;i++){
for(int j=0;j<=i;j++)
System.out.print(yanghui(i,j)+" ");
System.out.println();
} //输出
}
}

转载于:https://www.cnblogs.com/yizanboke/p/5951846.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值