【问题标题】:quadratic formula with scanner inputs具有扫描仪输入的二次公式
【发布时间】:2013-04-09 11:17:15
【问题描述】:

好的,所以我是一个完整的 Java 菜鸟,我正在尝试为使用扫描仪输入运行二次方程的类创建一个程序。到目前为止,我得到的是这样的:

import java.util.*;

public class QuadraticFormulaSCN {


public static void main(String[]args) {
  System.out.println("insert value for a:");
  Scanner scan1 = new Scanner(System.in);
   double a = scan1.nextDouble();
    System.out.println("insert value for b:");
  Scanner scan2 = new Scanner(System.in);
    double b = scan2.nextDouble();
    System.out.println("insert value for C:");
  Scanner scan3 = new Scanner(System.in);
   double c = scan3.nextDouble();

    double answer =((Math.sqrt(Math.pow(b,2)-(4*a*c))-b)/2);
      double final2 =(-b + Math.sqrt(Math.pow(b,2)-(4*a*c)))/2;
  System.out.println("The x values are:" + answer + final2);
}
}

但是我得到了一个奇怪的输出,特别是NaNaN...我该怎么做才能解决这个问题?我做错了什么?

【问题讨论】:

  • 你应该只使用一个Scanner。
  • 此外,如果您(至少出于测试目的)避免单行代码并将微积分分隔为多个变量,以便于调试打印,它将对您有所帮助。无论如何,您确定您设置的方程至少有一个解(恕我直言,如果它们确实存在,您应该尝试打印这两个解)。

标签: java input java.util.scanner output quadratic


【解决方案1】:

我回答有点晚了,但我纠正了你的问题(在其他答案中描述),修正了你的一个计算,并清理了你的代码。

import java.util.*;

public class Test {

    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Insert value for a: ");
        double a = Double.parseDouble(s.nextLine());
        System.out.println("Insert value for b: ");
        double b = Double.parseDouble(s.nextLine());
        System.out.println("Insert value for c: ");
        double c = Double.parseDouble(s.nextLine());
        s.close();

        double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
        double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);

        if (Double.isNaN(answer1) || Double.isNaN(answer2))
        {
            System.out.println("Answer contains imaginary numbers");
        } else System.out.println("The values are: " + answer1 + ", " + answer2);
    }
}

【讨论】:

    【解决方案2】:

    NaN 是计算无效时得到的。比如除以0或者取-1的平方根。

    当我使用a = 1、b = 0 和c = -4 测试您的代码时,答案是2.02.0

    格式不对,final2的计算没有取反。

    否则代码是正确的。

    为了改进,您可以检查判别式是否为负。

    double d = b*b -4 * a * c;
    if (d < 0){
       System.out.println("Discriminant < 0, no real solutions" );
       return;
    }
    double x1 = (-b  -sqrt(d))/(2*a);
    double x2 = (-b +sqrt(d))/(2*a);
    System.out.format("The roots of your quadratic formula are %5.3f and %5.3f\n",x1,x2);
    

    或者,如果您更喜欢支持复杂领域的解决方案:

    if (d < 0) {
        System.out.println("Discriminant < 0, only imaginary solutions");
        double r = -b / (2 * a);
        double i1 = -sqrt(-d) / (2 / a);
        double i2 = sqrt(-d) / (2 / a);
        System.out.format("The roots of your quadratic formula are (%5.3f + %5.3fi) and (%5.3f + %5.3fi)\n",r, i1, r, i2);
        return;
    }
    

    【讨论】:

      【解决方案3】:

      您收到NaN 是因为您试图取负数的平方根。在数学中这是不允许的,除非你允许复数,例如1 +/- 2i.

      当判别式(平方根中的事物)为负时,这可能发生在二次公式中,例如x^2 + 6*x + 100:b^2 - 4ac = 36 - 400 = -364。取负数in Java leads to NaN 的平方根。 (不是数字)

      要测试NaN,请使用Double.isNaN 并适当处理NaN。

      此外,即使没有遇到NaN,您的计算也是不正确的:

      $ java QuadraticFormulaSCN
      insert value for a:
      1
      insert value for b:
      5
      insert value for C:
      6
      The x values are:-2.0-2.0
      

      这应该输出 2.0 和 3.0

      【讨论】:

        【解决方案4】:

        您应该只在以下情况下进行计算 判别式等于或大于零

        if(((Math.pow(b,2)-(4*a*c))>= 0){ /* Calculation here */ } 
        else {/*error message or complex number calculus*/};
        

        【讨论】:

          【解决方案5】:

          我总是尝试做的一件事是将我所有的数学都放在适当的括号中,以避免太容易出现运算顺序错误。 NaN 表示“不是数字”。如果用户输入的数字无法产生结果,您也会收到该消息,例如尝试获取负数的平方根。另外,就像一个注释一样,您可以通过仅在扫描仪上使用 a、b 和 c 来节省一些时间。

          public class QuadraticFormula{
          
             public static void main(String[] args){
                java.util.Scanner input = new java.util.Scanner(System.in);
                double a = input.nextDouble();
                double b = input.nextDouble();
                double c = input.nextDouble();
          
                double quadPos = (-b + Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
                double quadNeg = (-b - Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
          
                System.out.println("-b - = " + quadNeg + "\n-b + = " + quadPos);
          
                }
             }
          

          【讨论】:

            猜你喜欢
            • 2015-03-22
            • 2019-11-08
            • 2014-07-26
            • 1970-01-01
            • 1970-01-01
            • 2017-09-04
            • 1970-01-01
            • 2017-05-30
            • 2016-06-12
            相关资源
            最近更新 更多