二次方程式程序-出现NaN错误 [英] Quadratic Formula Program - getting NaN error

查看:118
本文介绍了二次方程式程序-出现NaN错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的代码在做什么错,但是我无法使其正常运行……测试人员不断返回NaN而不是预期的结果.本练习的目的是打印二次方程式的所有实际解. solution1应该返回较小的解决方案,而solution2应该返回较小的解决方案.这是我的课.

I'm not sure what I'm doing wrong in my code but I can't get it to run properly...the tester keeps returning NaN instead of what's supposed to be expected. The goal of this exercise is to print all real solutions to the quadratic equation. solution1 should return the smaller solution and solution2 should return the smaller solution. Here's my class.

   public class QuadraticEquation
   {
    private int a;
    private int b;
    private int c;
    private boolean hasSolutions;
    private double discriminant = (b * b) - (4 * a * c);
    private double solution1;
    private double solution2;
   /**
 * Constructs a quadratic equation
 * @param a coefficient a 
 * @param b coefficient b
 * @param c coefficient c
 */

    public QuadraticEquation(int a, int b, int c)
    {
        a = a;
        b = b;
        c = c;
    }
     /**
 * Checks if there is a solution
 * @return true if there is a real solution
 */

    public boolean hasSolutions()
    {
        if(discriminant < 0)
            hasSolutions = false;
        else
            hasSolutions = true;
        return hasSolutions;
    }
    /**
  * Returns first solution
  * @return first solution
  */

    public double getSolution1()
    {
        if(hasSolutions)
        {
            if((-b + Math.sqrt(discriminant) / (2 * a)) < (-b - Math.sqrt(discriminant) / (2 * a)))
                solution1 = (-b + Math.sqrt(discriminant) / (2 * a));
            else
                solution1 = (-b - Math.sqrt(discriminant) / (2 * a));
        }
            return solution1;
    }
    /**
    * Returns second solution
    * @return second solution
    */  

    public double getSolution2()
    {
        if(hasSolutions)
        {
            if((-b + Math.sqrt(discriminant) / (2 * a)) > (-b - Math.sqrt(discriminant) / (2 * a)))
                solution2 = (-b + Math.sqrt(discriminant) / (2 * a));
            else
                solution2 = (-b - Math.sqrt(discriminant) / (2 * a));
        }
            return solution2;
    }

  }

这是我的测试人员:

    public class QuadraticEquationTester
    {
    public static void main(String[] args)
      {
          QuadraticEquation equation = new QuadraticEquation(2, 2, -4);
          System.out.println(equation.hasSolutions());
          System.out.println("Expected: true");
          System.out.println(equation.getSolution1());
          System.out.println("Expected: -2");
          System.out.println(equation.getSolution2());
          System.out.println("Expected: 1");

          QuadraticEquation equation2 = new QuadraticEquation(2, 2, 4);
          System.out.println("\n" + equation2.hasSolutions());
          System.out.println("Expected: false");
          System.out.println(equation2.getSolution1());
          System.out.println("Expected: 0");
          System.out.println(equation2.getSolution2());
          System.out.println("Expected: 0");
        }
     }

非常感谢! :)

推荐答案

  1. 您正在用默认值a, b, and c计算的类上下文的对象初始化部分中计算discriminant.在初始化a,bc

  1. You are computing discriminant in the object initializing section of the class context which is being computed with default value of a, b, and c. Do not compute until initializing a,b, and c

由于a, b, c是参数字段,因此您正在构造函数方法中隐藏变量.使用this:

You are hiding variable in your constructor method as a, b, c are parameter fields. Use this:

public QuadraticEquation(int a, int b, int c)
{
  this.a = a;
  this.b = b;
  this.c = c;
}

  • NaN的结果用于浮点计算,其运算类似于将零逐个零.因此,找出上述修复是否仍然会发生这种情况.

  • The NaN results for floating point computation with an operation like deviding a zero by zero. SO find out such if still it happens with the above fix.

    这篇关于二次方程式程序-出现NaN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆