我毫无希望地试图用(int = amountCoefficients)参数数量来调用一个函数,并用这些参数声明一个函数。如何调用和声明具有可变数量参数的方法?
是什么让它更难的是,例如amountCoefficients = 5,那么它意味着有5个块的数组,每个都有一个值(双)。所以第一个参数必须等于该数组的第一个块的值,第二个参数必须等于该数组的第二个块的值等。
而事先我们不需要知道我们需要多少个参数,因为这取决于用户填入的双倍数量,所以amountCoefficients可以等于2,4或任何其他正整数。
我对Java很新,我真的不知道该怎么做。正如你在下面看到的,我试图用for循环做一些事情,但我不认为这是有效的。
public class Interpol {
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
//acquire user input (polynomial coefficients and interval values x1 and x2)
ArrayList<Double> polynomialCoefficients = new ArrayList<Double>();
int amountCoefficients = 0;
while (scanner.hasNextDouble()) {
polynomialCoefficients.add(scanner.nextDouble());
amountCoefficients++;
}
String in = scanner.next();
double x1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
//call method f to determine the polynomial function
int i = 0;
for (i = 0; i < amountCoefficients; i++) {
f
}
//call method findaroot to determine the root
//print result
}
}
public static double f(double x) {
//function of which a root is to be found
}
感谢您的解决方案! – Ken 2014-09-20 01:17:28