好的,所以我必须创建一个 nCr 程序。
我需要允许用户输入他们希望计算的 nCr 值的数量(包括 2 到 12 之间)。然后相应地输入 n 和 r 值,确保 n 大于 r (n > r) 然后我需要计算 nCr 值并将其存储在数组中并输出值
我希望以二维数组的格式输出值,格式如下:
n | c | 铬
(without the grids)
到目前为止,我的代码如下:
public class nCr {
public static void main (String[] args) {
int nCrValues;
System.out.println("Enter amount of nCr values you wish to calculate: ");
nCrValues = input();
while ((nCrValues < 2) || (nCrValues > 10)){
System.out.println("Must be between 2 and 12 inclusive");
nCrValues = input();
}//END while
values(nCrValues);
}//END main
public static void values(int nCrValues){
//I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
int n, r;
System.out.println("Enter n value: ");
n = input();
System.out.println("Enter r value: ");
r = input();
calculatenCr(n,r);
}//END values
public static int calculatenCr(int n, int r){
int nCr;
nCr = fact(n) / (fact(n-r) * fact(t));
//store nCr values in array
}//END calculatenCr
public static int fact(int num){
int count;
int factor = 1;
for (count = 1; count <= num; count++){
factor *= count;
}//END for
return factor;
}//END fact
public static int input(){
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while (input < 0){
System.out.println("Must be a positive number.")'
input = sc.nextInt();
}//END while
return input;
}//END input
}//END CLASS
我需要一种将数组输出为表单的方法
n | c | nCr |
我该怎么做,到目前为止我的所有编码都是正确的吗?
非常感谢