问题
For my programming class I have to make a program that calculates the combinations function C(n,k) = n!/(k!*(n-k)!) by using a main method to deal with input/output, a method to compute the factorial, and a method to compute the combinations function. Here's my code:
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
My problem is that I'm getting an error message for System.out.println(factorials(s,o));
- "The method println(boolean) in the type PrintStream is not applicable for the arguments (void)". I have no idea why it's saying this. Help?
回答1:
In your code,
public static void factorials(int set, int objects)
{
}
returns void
. you cannot use void
method in
System.out.println(factorials(s, o));
try chaning your code as
public static int factorials(int set, int objects)
{
}
another way to calculate combinations without method calling,
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);
Output:
Number Of Items (n) :
8
Items to be selected (r):
3
Combinations : 8C3 : 56
回答2:
Basically you are passing an argument to your println(Object)
method. But factorials(s,o)
returns a void. You can either call println()
or else if you want to use println(Object)
then factorials(s,o)
should return a descendent of java.lang.Object.
Refer to specifications for further understanding :
PrintStream
来源:https://stackoverflow.com/questions/19532553/java-program-to-calculate-the-combinations-function