我毫無希望地試圖用(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