创建一个java程序来解决二次方程 [英] Create a java program to solve quadratic equations

查看:23
本文介绍了创建一个java程序来解决二次方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

求解二次方程

到目前为止,我已经写下了以下内容.不知道怎么介绍第二种方法

public static void main(string args[]){

}

public static  double quadraticEquationRoot1(int a, int b, int c) (){

}

if(Math.sqrt(Math.pow(b, 2) - 4*a*c) == 0)
{
    return -b/(2*a);
} else {
    int root1, root2;
    root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    return Math.max(root1, root2);  
}

}

推荐答案

首先,你的代码不会编译——你在 public static double quadraticEquationRoot1 开始后多了一个 }(int a, int b, int c) ().

Firstly, your code won't compile--you have an extra } after the start of public static double quadraticEquationRoot1(int a, int b, int c) ().

其次,您没有在寻找正确的输入类型.如果您需要 double 类型的输入,请确保正确声明该方法.当它们可能是双精度时(例如,root1root2),也要小心声明为 int.

Secondly, you aren't looking for the correct input types. If you want input of type double, make sure you declare the method appropriately. Also be careful of declaring things as int when they could be doubles (for example, root1 and root2).

第三,我不知道你为什么有 if/else 块——最好跳过它,只使用当前在 else 中的代码 部分.

Thirdly, I don't know why you have the if/else block--it would be better to simply skip it, and only use the code that is currently in the else part.

最后,解决您的原始问题:只需创建一个单独的方法并使用 Math.min() 而不是 Math.max().

Finally, to address your original question: Simply create a separate method and use Math.min() instead of Math.max().

所以,回顾一下代码:

public static void main(string args[]){

}

//Note that the inputs are now declared as doubles.
public static  double quadraticEquationRoot1(double a, double b, double c) (){    
    double root1, root2; //This is now a double, too.
    root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    return Math.max(root1, root2);  
}

public static double quadraticEquationRoot2(double a, double b, double c) (){    
    //Basically the same as the other method, but use Math.min() instead!
}

这篇关于创建一个java程序来解决二次方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆