【问题标题】:NaN Error w/ Quadratic Equation Calculator二次方程计算器的 NaN 误差
【发布时间】:2015-03-12 02:57:53
【问题描述】:

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

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

}

【问题讨论】:

  • 如果您在帖子中提供其他详细信息,例如 abc 在 @987654325 时是什么,您应该能够避免“帖子主要是代码”消息@ 出现。我怀疑NaN 是由这样的输入产生的,这些输入描述了一个没有实根的二次方程。当参数为负时,Math.sqrt 返回NaN
  • 欢迎来到 StackOverflow。这个网站非常有用,但确保您正确使用它也很重要。您应该查看站点导览,以确保您了解这里的工作原理,但只是为了确保您不会被否决 - 编辑您的帖子并尝试提供尽可能多的详细信息并尽可能具体您可以为了得到最好和最准确的支持
  • 请编辑问题并解释为什么它不起作用。
  • 什么时候出现 nan 错误?一些输入?每个输入?对于应该有虚根的输入?对于不应该有虚根的输入?什么是 com.mtndewey.calculator?这篇文章主要是代码”警告在这里非常贴切。

标签: java calculator nan quadratic


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 2020-11-28
    • 2015-07-08
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多