[英]quadratic formula with scanner inputs
好的,所以我是一个完整的Java新手,我正在尝试为使用扫描仪输入运行二次方程的类创建程序。 到目前为止,我所得到的是:
import java.util.*;
public class QuadraticFormulaSCN {
public static void main(String[]args) {
System.out.println("insert value for a:");
Scanner scan1 = new Scanner(System.in);
double a = scan1.nextDouble();
System.out.println("insert value for b:");
Scanner scan2 = new Scanner(System.in);
double b = scan2.nextDouble();
System.out.println("insert value for C:");
Scanner scan3 = new Scanner(System.in);
double c = scan3.nextDouble();
double answer =((Math.sqrt(Math.pow(b,2)-(4*a*c))-b)/2);
double final2 =(-b + Math.sqrt(Math.pow(b,2)-(4*a*c)))/2;
System.out.println("The x values are:" + answer + final2);
}
}
但是我得到一个奇怪的输出,特别是NaNaN
...我该怎么做才能解决这个问题? 我究竟做错了什么?
NaN
是您在计算无效时得到的东西。 例如除以0或取-1的平方根。
当我使用a = 1
, b = 0
和c = -4
测试您的代码时,答案是2.02.0
格式不正确, final2
的计算未取反。
否则,代码是正确的。
为了提高效率,您可以检查判别式是否为负。
double d = b*b -4 * a * c;
if (d < 0){
System.out.println("Discriminant < 0, no real solutions" );
return;
}
double x1 = (-b -sqrt(d))/(2*a);
double x2 = (-b +sqrt(d))/(2*a);
System.out.format("The roots of your quadratic formula are %5.3f and %5.3f\n",x1,x2);
或者,如果您希望支持复杂领域的解决方案:
if (d < 0) {
System.out.println("Discriminant < 0, only imaginary solutions");
double r = -b / (2 * a);
double i1 = -sqrt(-d) / (2 / a);
double i2 = sqrt(-d) / (2 / a);
System.out.format("The roots of your quadratic formula are (%5.3f + %5.3fi) and (%5.3f + %5.3fi)\n",r, i1, r, i2);
return;
}
我来晚了一点,但是我纠正了您的问题(在其他答案中进行了描述),修复了其中一个计算,并清理了代码。
import java.util.*;
public class Test {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Insert value for a: ");
double a = Double.parseDouble(s.nextLine());
System.out.println("Insert value for b: ");
double b = Double.parseDouble(s.nextLine());
System.out.println("Insert value for c: ");
double c = Double.parseDouble(s.nextLine());
s.close();
double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
if (Double.isNaN(answer1) || Double.isNaN(answer2))
{
System.out.println("Answer contains imaginary numbers");
} else System.out.println("The values are: " + answer1 + ", " + answer2);
}
}
之所以得到NaN
是因为您试图取负数的平方根。 在数学中这是不允许的,除非您允许使用复数,例如1 +/- 2i
。
当判别式(平方根中的事物)为负时,这可能在二次公式中发生,例如x^2 + 6*x + 100
: b^2 - 4ac
ac = 36-400 = -364。 在Java中取负数的平方根会导致NaN
。 (不是数字)
要测试NaN
,请使用Double.isNaN
并适当处理NaN
。
此外,即使没有遇到NaN
,您的计算也不正确:
$ java QuadraticFormulaSCN
insert value for a:
1
insert value for b:
5
insert value for C:
6
The x values are:-2.0-2.0
这应该已经输出了2.0和3.0
仅当判别式等于或大于零时才应进行计算
if(((Math.pow(b,2)-(4*a*c))>= 0){ /* Calculation here */ }
else {/*error message or complex number calculus*/};
我一直试图做的一件事是将我所有的数学都放在适当的括号内,以免出现过于简单的操作顺序错误。 NaN在说“不是数字”。 如果用户输入无法产生结果的数字,例如尝试获取负数的平方根,您也会收到该消息。 另外,仅作说明,您可以通过仅在Scanner上使用a,b和c来节省时间。
public class QuadraticFormula{
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double quadPos = (-b + Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
double quadNeg = (-b - Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
System.out.println("-b - = " + quadNeg + "\n-b + = " + quadPos);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.