带有二次方程计算器的 NaN 错误

NaN Error w/ Quadratic Equation Calculator

尝试使此代码正常工作时遇到问题!请帮忙!

package com.mtndewey.calculator;

import java.util.Scanner;

public class Calculator {

private static double inputA, inputB, inputC;

public static void main(String[] args) {

    final Scanner ScannerA = new Scanner(System.in);
    final Scanner ScannerB = new Scanner(System.in);
    final Scanner ScannerC = new Scanner(System.in);

    System.out.println("Enter the A value");
    final double inputA = ScannerA.nextDouble();
    System.out.println("Enter the B value");
    final double inputB = ScannerA.nextDouble();
    System.out.println("Enter the C value");
    final double inputC = ScannerA.nextDouble();

    getComponents(inputA, inputB, inputC);
    double[] answers = getAnswers();

    if (answers != null) {
        if (inputA == 0 && inputB != 0) {// if they input a linear equation
            System.out.print("This is a linear equation, x equals "
                    + (-inputC / inputB) + ".");
            System.exit(0);
        }
        if (inputA == 0 && inputB == 0 && inputC != 0) {// if they put just
                                                        // the constant
            System.out.print("No solution.");
            System.exit(0);

        }
        if (inputA == 0 && inputB == 0 && inputC == 0) {// if they put only
                                                        // zeros
            System.out.print("Zero equals zero.");
            System.exit(0);
        }
        if (answers[0] == answers[1] && inputA != 0) {// outputs only one
                                                        // for no repetition
            System.out.println("x equals: ");
            System.out.print(answers[0]);
            System.exit(0);
        }
        if (answers[0] != answers[1] && inputA != 0) {// normal input
            System.out.println("x equals: " + answers[0] + ", " + answers [1]);

        }

        else {
            System.out.println("Error");
        }
    }
    if (answers == null) {
        System.out.print("Imaginary answer!");
    }
}

public static double[] getComponents(double a, double b, double c) {

    double discriminant = (b * b) - (4 * a * c);
    double component1 = b * -1; // negative b
    double component2 = Math.sqrt(discriminant);
    double component3 = component2 / (2 * a);
    double[] components = { component1, discriminant, component3 };

    return components;

}

public static boolean isImaginary() {
    double[] components = getComponents(inputA, inputB, inputC);
    double discriminant = components[1];

    if (discriminant < 0) {
        return true;
    } else
        return false;
}

public static double[] getAnswers() {
    double[] components = getComponents(inputA, inputB, inputC);
    double component1, component3;
    component1 = components[0];
    component3 = components[2];

    if (isImaginary()) {
        return null;
    } else {// Answers

        double answerplus = component1 + component3;
        double answerminus = component1 - component3;
        double[] answers = { answerplus, answerminus };

        return answers;
    }
}

}

出于某种原因,您有 2 组同名变量。

您有 static 个变量 inputAinputBinputC 从未分配(因此具有默认值 0.0)。

您还有实例变量 inputAinputBinputC。这些是使用您输入的值分配的。

不幸的是,无论您输入什么值,getAnswers 都会使用 static 版本,因此 component3 是根据 0.0/0.0 计算得出的,即 NaN(不是数字).