[英]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.