gpt4 book ai didi

java - 从 Java 中的方法返回 double /整数?

转载 作者:行者123 更新时间:2023-12-02 05:11:04 24 4
gpt4 key购买 nike

我正在尝试编写接受一组数字的代码,通过二次公式运行它们并返回然后打印的答案。

附注我是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 +".");

您的函数 posquadFormnegquadForm 已经计算出答案,您只需将它们存储在变量中并打印出来?

关于java - 从 Java 中的方法返回 double /整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com