NaN误差w /二次方程计算器

时间:2015-01-14 01:02:25

标签: java calculator nan quadratic

无法尝试使此代码正常工作!请帮忙!

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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

由于某种原因,你有两组具有相同名称的变量。

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

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

很遗憾,无论您输入什么值,getAnswers都会使用static版本,因此component3的计算结果为0.0/0.0 NaN(非数字)。