简体   繁体   English

二次方程求解器无法在Java中获得结果

[英]quadratic equation solver fails to get result in Java

I have code that is supposed to solve a quadratic equation but yields NaN as a result. 我有应该解决二次方程式但产生NaN结果的代码。

I've looked around for 2 days now and I can't find a solution. 我已经环顾了两天,但找不到解决方案。 Any and all advice will be more than appreciated! 任何和所有建议将不胜感激!

package quadratic;

import java.util.Scanner;

public class Formlua {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("enter value of A ");
        double a = input.nextDouble();
        System.out.println("enter value of B ");
        double b = input.nextDouble();
        System.out.println("enter value of C ");    
        double c = input.nextDouble();
        double four = 4;
        double square = Math.sqrt(b* b - 4 * a * c );

        double root1 = (-b + square) / (2*a);

        double root2 = (-b - square) / (2*a);   
        System.out.println("The answer is " + root1 + "and" + root2);

        System.out.println("Do you want to continue? y/n");

        String user = input.toString();
        if(user.equalsIgnoreCase("y"));
    }
}

This code: 这段代码:

Math.sqrt(b* b - 4 * a * c );

can result in NaN ("not a number"). 可能会导致NaN (“非数字”)。

If the value of b* b - 4 * a * c is negative, there are solutions only in complex numbers (but not in double data type) 如果b* b - 4 * a * c值为负,则仅存在复数形式的解决方案(而不是双精度数据类型)

There should be a condition 应该有条件

if (b* b - 4 * a * c<0) {
    System.out.println("There is no solution in real numbers");
    return;
}

The most likely cause of the problem is Math.sqrt(b*b - 4 * a * c). 该问题最可能的原因是Math.sqrt(b * b-4 * a * c)。 or one of your input values is NaN (probably not the cause in this situation). 或您输入的值之一是NaN(在这种情况下可能不是原因)。

There are two special cases: b *b < 4 * a * c and a = 0 有两种特殊情况:b * b <4 * a * c和a = 0

if b * b < 4 * a * c your answer is in the complex plane (specifically, not a real number). 如果b * b <4 * a * c,则您的答案在复平面上(具体来说,不是实数)。 if a = 0 then you actually just have a linear equation. 如果a = 0,那么您实际上只有一个线性方程。

you could try the following code: 您可以尝试以下代码:

package quadratic;
import java.util.Scanner;
public class Formlua {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("enter value of A ");
    double a = input.nextDouble();

    System.out.println("enter value of B ");
    double b = input.nextDouble();

    System.out.println("enter value of C ");    
    double c = input.nextDouble();

    if (a == 0){
      // 0 = 0*x*x + b*x + c  ==> x = -c/b
      System.out.println("X = " + Double.toString(-c/b));
    } else {
      double inner = b * b - 4 * a * c;
      if (inner < 0){
        inner = -inner;
        inner = Math.sqrt(inner);
        System.out.println("X = " + Double.toString(-b) + " + " + Double.toString(inner) + "i")
        System.out.println("  = " + Double.toString(-b) + " - " + Double.toString(inner) + "i");
      } else {
         inner = Math.sqrt(inner);
           System.out.println("X = " + Double.toString(-b));
         if (inner == 0){
         } else {
           System.out.println("X = " + Double.toString(-b + inner));
           System.out.println("X = " + Double.toString(-b - inner));
      }
    }
  }
}

This lets your user input any double values and recieve an answer. 这使您的用户可以输入任何双精度值并获得答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM