我正在尝试编写接受一组数字的代码,通过二次公式运行它们并返回然后打印的答案。
附注我是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;
}
最佳答案
更改为
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
已经计算出答案,您只需将它们存储在变量中并打印出来?
关于java - 从 Java 中的方法返回 double /整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339470/