-3

好的,所以我必须创建一个 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 |

我该怎么做,到目前为止我的所有编码都是正确的吗?

非常感谢

4

1 回答 1

0

如果您只想打印组合数量而不是实际组合本身,则不完全理解,但这就是您的代码所指示的,所以这就是我完成它的方式:

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
        for (int[] res : values(nCrValues)) {
            System.out.println(String.format("%d | %d | %d", res[0], res[1], res[2]));
        }
    }//END main

    public static int[][] values(int nCrValues){

        //I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
        int[][] res = new int[nCrValues][3];
        for (int i = 0; i < nCrValues; i++) {
            int n, r;

            System.out.println("Enter n value: ");
            n = input();
            System.out.println("Enter r value: ");
            r = input();

            res[i] = new int[]{n, r, calculatenCr(n, r)};
        }
        return res;
    }//END values

    public static int calculatenCr(int n, int r){
        return fact(n) / (fact(n-r) * fact(r));
    }//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
于 2017-05-01T10:33:44.587 回答