Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java中的方法返回双精度/整数?_Java - Fatal编程技术网

从Java中的方法返回双精度/整数?

从Java中的方法返回双精度/整数?,java,Java,我试图编写一个代码,将一组数字输入,通过二次公式运行它们,然后返回答案并打印出来 另外,我是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(); Sc

我试图编写一个代码,将一组数字输入,通过二次公式运行它们,然后返回答案并打印出来

另外,我是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
已经计算了答案,您只需要将它们存储在变量中并打印出来?

您的方法声明如下:

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));
旁注,而不是:

int b;
b = (bx);
int a;
a = (ax);
您只需使用:

int b = bx;
int a = ax;
与Alex K的答案相反的是不取任何参数,只将ax、bx和c作为全局变量处理(假设四元公式类是一个内部类)


“二次_公式中的posquadForm()不能应用于:*传递到方法中的整数列表”。根据想法。
public double-posquadForm(intax,intbx,intc)
接受3个整数,但当您调用
System.out.println(“您的答案是“+work.posquadForm()+”或“+work.negquadForm()+”),您没有传入任何整数。
public double posquadForm() {

    double posanswer;
    posanswer = ((-bx) - Math.sqrt((bx^2) + ((-4) * ax * c)) / (2 * ax));
    return posanswer;
}