import java.util.Scanner;
class Suanfa{
public int diedaiSuanfa(int n , int m) {
if(n==0||n<m)
return 0;
else if(n==m||m==0)
return 1;
else if(m==1)
return n;
else
return diedaiSuanfa(n-1, m-1)+diedaiSuanfa(n-1,m);
}
}
public class Diedai {
public static void main(String args[]) {
Suanfa sum=new Suanfa();
System.out.print("请输入n,m(回车确认):");
Scanner reader=new Scanner(System.in);
int a=reader.nextInt();
int b=reader.nextInt();
System.out.println("C("+a+","+b+")="+sum.diedaiSuanfa(a,b));
}
}
正常情况
边界情况
异常情况
JDB调试下,求C(10,2)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Dieda
问题int [] a = new int [args.length];
for(int i=0; i<args.length;i++) {
a[i] = Integer.parseInt(args[i]);
}
更换了输入数据的形式后为:
Scanner reader=new Scanner(System.in);
int a=reader.nextInt();
int b=reader.nextInt();
顺利编译、运行
原文:https://www.cnblogs.com/zxy20175227/p/10624120.html