[假设一个Java程序从用户那里得到了一个多项式方程,例如“ 3x ^ 2-4x ^ 3 + 3x ^ 3”,然后又得到了另一个数字“ x”,然后用给定的x求解给定的方程并打印结果。如果您有任何想法或解决方案,我将非常感激。非常感谢!
输入:3x ^ 2-4x ^ 3 + 3x ^ 3x = 4输出:-16输入:3x-4x ^ 2 + 2x ^ 4x = 2输出:22
public static double polynomialFunction(double[] factors, double[] exponents, double x) {
double result = 0.0;
for (int i = 0; i < factors.length; i++) {
result += factors[i] * Math.pow(x, exponents[i]);
}
return result;
}
public static void main(String[] args) {
double[] factors = {3.0, -4.0, 3.0};
double[] exponents = {2.0, 3.0, 3.0};
double x = 4.0;
System.out.println(polynomialFunction(factors, exponents, x));
}