我正在尝试创建一组带有一组数字的代码,通过二次公式运行它们并返回然后打印的答案。
P.S。我是java新手,这是为了学习。
Scanner firstCoeff = new Scanner(System.in);
int ax = firstCoeff.nextInt();
firstCoeff.close();
Scanner secCoeff = new Scanner(System.in);
int bx = secCoeff.nextInt();
secCoeff.close();
Scanner finConstant = new Scanner(System.in);
int c = finConstant.nextInt();
Quadratic_Formula work = new Quadratic_Formula();
work.posquadForm(ax, bx, c);
work.negquadForm(ax, bx, c);
System.out.println("Your answer is" + work.posquadForm() +"or" + work.negquadForm() +".");
这是公式类:
public class Quadratic_Formula {
public double posquadForm(int ax, int bx, int c) {
int b;
b = (bx);
int a;
a = (ax);
double posanswer;
posanswer = ((-b) - Math.sqrt((b^2) + ((-4) * a * c)) / (2 * a));
return posanswer;
}
public double negquadForm(int ax, int bx, int c) {
int b;
b = (bx);
int a;
a = (ax);
double neganswer;
neganswer = ((-b) + Math.sqrt((b^2) + ((-4) * a * c)) / (2 * a));
return neganswer;
}
答案 0 :(得分:2)
更改为
Quadratic_Formula work = new Quadratic_Formula();
double posAnswer = work.posquadForm(ax, bx, c);
double negAnswer = work.negquadForm(ax, bx, c);
System.out.println("Your answer is" +posAnswer +"or" + negAnswer +".");
您的职能posquadForm
& negquadForm
已经计算了答案,你只需要将它们存储在变量中并打印出来吗?
答案 1 :(得分:0)
您的方法声明如下:
public double posquadForm(int ax, int bx, int c) {
所以只需传递这些变量......
int valueForAx = 2;
int valueForBx = 3;
int valueForC = 4;
System.out.println("Your answer is " + work.posquadForm(valueForAx, valueForBx, valueForC));
答案 2 :(得分:0)
旁注,而不是:
int b;
b = (bx);
int a;
a = (ax);
你只需使用:
int b = bx;
int a = ax;
与Alex K的答案相反的是不接受任何参数,只需将ax,bx和c作为全局变量处理(假设四元公式类是内部类)。
public double posquadForm() {
double posanswer;
posanswer = ((-bx) - Math.sqrt((bx^2) + ((-4) * ax * c)) / (2 * ax));
return posanswer;
}