【问题标题】:Java program to calculate the combinations function计算组合函数的Java程序
【发布时间】:2013-10-23 13:47:28
【问题描述】:

对于我的编程课,我必须编写一个程序,通过使用 main 方法处理输入/输出来计算组合函数 C(n,k) = n!/(k!*(n-k)!),a计算阶乘的方法,以及计算组合函数的方法。这是我的代码:

import java.util.Scanner;
public class Combinations {

    public static void factorials (int set, int objects) {
        Scanner keyboard = new Scanner(System.in);
        int n = set;
        int k = objects;
        int c = (n-k);
        int factorial1 = 1;
        int factorial2 = 1;
        int factorial3 = 1;
        while (n > 0) {
            factorial1 = factorial1 + s;
            n = n++;
        }//while loop
        while (k > 0) {
            factorial2 = factorial2 + o;
            k = k++;
        }//while loop
        while (c > 0) {
            factorial3 = factorial3 + c;
            c = c++;
        }//while loop
        System.out.println(Combinations(factorial1,factorial2,factorial3));
    }//method factorials
    public static int Combinations (int set, int objects, int x){
        int n = set;
        int k = objects;
        int c = x;
        int combination;
        combination = n/(k*c);
        return combination;
    }//method Combinations
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the number of integers in a set: ");
        int n = keyboard.nextInt();
        System.out.println("Enter the number of objects to be chosen from the set: ");
        int k = keyboard.nextInt();
        System.out.println(factorials(n,k));
    }//method main

}//class

我的问题是我收到System.out.println(factorials(s,o)); 的错误消息 - “PrintStream 类型中的方法 println(boolean) 不适用于参数 (void)”。我不知道为什么会这样说。帮助?

【问题讨论】:

    标签: java methods combinations factorial


    【解决方案1】:

    在您的代码中,

    public static void factorials(int set, int objects)
    {
    }
    

    返回void。你不能使用void 方法 System.out.println(factorials(s, o));

    尝试将您的代码更改为

    public static int factorials(int set, int objects)
    {
    }
    

    另一种无需方法调用即可计算组合的方法,

            int index, numberOfItems, itemsToBeSelected, combinations;
            int factorial, temp, result;
    
            Scanner scanner = new Scanner(System.in);
            System.out.println("Number Of Items (n) : ");
            numberOfItems = scanner.nextInt();
    
            System.out.println("Items to be selected (r): ");
            itemsToBeSelected = scanner.nextInt();
    
    
            for (index = 1, factorial = 1; index <= numberOfItems; index++)    /* Calculate (numberOfItems) -> n! */
            {
                factorial = factorial * index;
            }
    
            for (index = 1, temp = 1; index <= itemsToBeSelected; index++)         /* Calculate (itemsToBeSelected) -> r! */
            {
                temp = temp * index;
            }
    
            for (index = 1, result = 1; index <= (numberOfItems - itemsToBeSelected); index++) /* Calculate (numberOfItems - itemsToBeSelected) -> (n-r)!*/
            {
                result = result * index;
            }
    
            combinations = factorial / (temp * result);
            System.out.println("Combinations : " + numberOfItems + "C" + itemsToBeSelected + " : " + combinations);
    

    输出:

    Number Of Items (n) : 
    8
    Items to be selected (r): 
    3
    Combinations : 8C3 : 56
    

    【讨论】:

    • 谢谢@Woody!但是,虽然这确实消除了我的错误消息,但当我尝试运行该程序时,它除了询问要从集合中选择的对象数量之外没有做任何事情。无论我输入什么数字或多少个数字,程序都不会终止。是不是因为我跑了多种方法才得到最终的输出?
    【解决方案2】:

    基本上,您将参数传递给您的 println(Object) 方法。但是factorials(s,o) 返回一个空值。你可以调用println(),或者如果你想使用println(Object),那么factorials(s,o)应该返回java.lang.Object的后代。

    请参阅规范以进一步了解:

    PrintStream

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多