多个nCr程序

时间:2017-05-01 09:59:51

标签: java arrays ncr

好的,所以我必须创建一个nCr程序。

我需要允许用户输入他们希望计算的nCr值的数量(在2到12之间)。 然后相应地输入n和r值,确保n大于r(n> r) 之后我需要计算并将nCr值存储在数组中并输出值

我希望以下列形式输出2d数组格式的值:

n | c | nCr的

(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 |

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

非常感谢

1 个答案:

答案 0 :(得分: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