用java编写二次方程,创建一个Java程序来解决二次方程

Solving a quadratic equation

I have the following written down so far. I am not sure on how to introduce the second method

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);

}

}

解决方案

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

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).

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.

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

So, to recap in code:

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!

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值