對於我的編程類,我必須通過使用main方法來計算組合函數C(n,k)= n!/(k!*(nk)!)處理輸入/輸出,計算階乘的方法以及計算組合函數的方法。這裏是我的代碼:Java程序來計算組合函數
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方法(布爾)不適用於參數(無效)」。我不知道爲什麼這麼說。幫幫我?
謝謝@Woody!然而,雖然這樣做的確消除了我的錯誤信息,但當我嘗試運行該程序時,它並沒有做任何事情要求從該集合中選擇多少個對象。無論輸入什麼數字或多少個數字,程序都不會終止。是因爲我運行多種方法來獲得最終輸出? – ameliafromafairytale
@ameliafromafairytale查看我更新的答案。 – Woody
謝謝!這真的清理了很多:) – ameliafromafairytale