2011-10-19 37 views
1

儘管程序工作得很好,但我的教授提到以下是一個邏輯錯誤,應該修復。我很難過,當判別式等於0時,是不是隻有一個根?幫助將真正被讚賞!無法在二次方程程序中找到「java邏輯錯誤」的解決方案

這是他提到的代碼:

 if(discrim == 0) 
    { 
     eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA); 
     System.out.println("This equation only has a single real root. Root = " + eq1root1); 

下面是完整的代碼:

import java.lang.Math; 
import javax.swing.JOptionPane; 

public class Assignment6 
{ 
    public static void main(String[] args) 
    { 
    String a, 
      b, 
      c; 
    double coefA, 
     coefB, 
     coefC, 
     discrim, 
     eq1root1, 
     eq1root2; 

    //Here the user is inputting the coefficients through a popup dialog box 
    //Then the entered Strings are being converted to floating point numbers. 

    a = JOptionPane.showInputDialog("Please enter a number for the quadratic coefficient a"); 
    coefA = Double.parseDouble (a);   
    b = JOptionPane.showInputDialog("Please enter a number for the quadratic coefficient b"); 
    coefB = Double.parseDouble (b); 
    c = JOptionPane.showInputDialog("Please enter a number for the quadratic coefficient c"); 
    coefC = Double.parseDouble (c); 

    //Here the coefficients that the user entered are being displayed. 

    System.out.println("Your coefficient a = " + coefA); 
    System.out.println("Your coefficient b = " + coefB); 
    System.out.println("Your coefficient c = " + coefC); 

    //The following "nested if" statement sorts out equations with only 1 root, 2 roots, and or no roots at all. 
    discrim = coefB*coefB - (4 * coefA * coefC); 
    if(discrim == 0) 
    { eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA); 
     System.out.println("This equation only has a single real root. Root = " + eq1root1); 
    } 
    else if (discrim > 0) 
    { eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA); 
    eq1root2 =((-1*coefB) - Math.sqrt(discrim))/(2 * coefA); 
    System.out.println("This equation has two real roots."); 
    System.out.println("Root 1 = " + eq1root1); 
    System.out.println("Root 2 = " + eq1root2); 
    } 
    else 
    { 
     System.out.println("This equation does not have any real roots."); 
    } 

} 
} 
+0

請標記功課 – alf

+0

這是一個古老的評論,而只是一個供參考我們在消除作業標籤的過程中不處理。所以,如果你看到作業標籤的問題,請刪除它! :-) – corsiKa

回答

4

唯一的錯誤,我可以看到的是,你並不真正需要做的:

eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA); 

您只能做

eq1root1 = -1*coefB/(2 * coefA); 

因爲你在內部,如果你知道這個descriminant是零。

就像在另一條評論中提到的一樣,你需要確保coefA與0不同,因爲那會導致你的代碼產生一個異常(你不能被0除)。雖然實際上不是是一個二次方程,但驗證它是很重要的。

+1

這很有道理!非常感謝!沒有意識到這一點-__-! – junaidkaps

+0

很高興有幫助。 :)如果它*確實*解決了你的問題,請考慮接受答案;) – pcalcao

4

想想這個方程在數學方面:

((-1*coefB) + Math.sqrt(discrim))/(2 * coefA); 

會發生什麼事coefA = 0

+1

右側變成undefined :-) – junaidkaps

+1

對於純數學,那個或無窮大。但是IIRC Java會拋出一個異常。無論哪種方式,這是一個特殊的情況,你需要額外的邏輯來處理。打印出某種錯誤信息 - 例如,「這不是二次方程式」。我之前爲此類錯誤標記了學生。 – Izkata

2

我只能猜這裏...什麼教授是可能試圖說的是,你不能真正使用==比較double s。

例如,

public static void main(String[] args) { 
    double v = 0; 
    for (int i = 0; i < 100; i++) 
     v += 0.01; 
    final double w = 1; 
    System.out.println("v = " + v); 
    System.out.println("w = " + w); 
    if (v - w != 0.0) { 
     System.out.println("difference: " + (v - w)); 
    } 
} 

會發布如下:

v = 1.0000000000000007 
w = 1.0 
difference: 6.661338147750939E-16 
2

考慮一下,如果雙打不準確代表數字正確會發生什麼:

C:\Documents and Settings\glowcoder\My Documents>java Assignment6 
Your coefficient a = 1.0 
Your coefficient b = 0.2 
Your coefficient c = 0.01 
This equation has two real roots. 
Root 1 = -0.09999999868291098 
Root 2 = -0.10000000131708903 

我寫這個例子與(x + .1)^2應該有1個解決方案。擴展是x^2 + .2x + .01

你也當a = 0

C:\Documents and Settings\glowcoder\My Documents>java Assignment6 
Your coefficient a = 0.0 
Your coefficient b = 1.0 
Your coefficient c = 1.0 
This equation has two real roots. 
Root 1 = NaN 
Root 2 = -Infinity