2013-10-23 118 views
1

對於我的編程類,我必須通過使用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方法(布爾)不適用於參數(無效)」。我不知道爲什麼這麼說。幫幫我?

回答

1

在你的代碼,

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

回報void。你不能在 System.out.println(factorials(s, o));

嘗試chaning代碼爲

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

另一種方式來計算,而不方法調用組合使用void方法,

 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 
+0

謝謝@Woody!然而,雖然這樣做的確消除了我的錯誤信息,但當我嘗試運行該程序時,它並沒有做任何事情要求從該集合中選擇多少個對象。無論輸入什麼數字或多少個數字,程序都不會終止。是因爲我運行多種方法來獲得最終輸出? – ameliafromafairytale

+0

@ameliafromafairytale查看我更新的答案。 – Woody

+0

謝謝!這真的清理了很多:) – ameliafromafairytale

0

基本上,您正在向您的println(Object)方法傳遞參數。但factorials(s,o)返回一個void。你可以調用println()或者如果你想使用println(Object)那麼factorials(s,o)應該返回一個java.lang.Object的後代。

參見規格做進一步的瞭解:

PrintStream