我需要实现二次公式,我的方法期望一个双数组,并且应该返回一个带有精确解的int数组,也就是说,当解是两个数时,int数组应该是两个字段长,当有只有一个解决方案号,int数组应该是一个字段长,当没有解决方案时,int数组应该为空。
我在java上很安静,并且在返回解决方案时遇到问题。 Eclipse sais:" quadsol无法解析为变量。"你能帮帮我吗 - 我的错误在哪里?
这是我的代码:
public static int[] quadraticFormula (double[] coefficients) {
int i = 0;
int j = 0;
double discriminant;
double solution1;
double solution2;
int[] quadsol = new int[j];
discriminant = (Math.pow(coefficients[i+1],2)-(4*coefficients[i]*coefficients[i+2]));
if(discriminant >= 0) {
solution1 = (((Math.sqrt(discriminant)) - (coefficients[i+1]))/2*coefficients[i]);
solution2 = ((-(Math.sqrt(discriminant)) - (coefficients[i+1]))/2*coefficients[i]);
if(solution1 == solution2) {
quadsol[j] = (int) solution1;
} else {
quadsol[j] = (int) solution1;
quadsol[j+1] = (int) solution2;
}
} else {
quadsol[j] = 0;
}
return quadsol;