无法在二次方程式程序中找到“java逻辑错误”的解决方案

时间:2011-10-19 21:06:46

标签: java math

尽管该程序运行良好,但我的教授提到以下是一个逻辑错误,应该修复。我很难过,当判别式等于0时,是不是只有一个根?帮助将真正受到赞赏!

这是他提到的代码:

     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."); 
   }

 }
}

4 个答案:

答案 0 :(得分:4)

我唯一能看到的错误是你真的不需要这样做:

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

你只能做

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

因为你在if里面,你知道判决书是零。

如同在另一条评论中提到的那样,您需要确保coefA与0不同,因为这会导致您的代码引发异常(您不能除以0)。虽然这不会实际是二次方程,但验证它是很重要的。

答案 1 :(得分:4)

从数学角度考虑这个等式:

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

coefA = 0时会发生什么?

答案 2 :(得分:2)

我只能在这里猜测......教授可能试图说的是你不能真正使用==来比较double

例如,

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

答案 3 :(得分: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