【问题标题】:I don't understand how to properly work with NaN in Java [duplicate]我不明白如何在 Java 中正确使用 NaN [重复]
【发布时间】:2021-12-29 08:04:06
【问题描述】:

我想知道是否有人能够理解这个程序没有按应有的方式返回 NaN 的原因。

我目前正在编写一个计算二次方程解的程序,但如果解不是实数,我希望它打印出“解不是实数”而不是 NaN。相反,它仍然继续打印 NaN。

这是我写的代码:

    import java.util.Scanner;
    import java.lang.*;
    
    public class QuadraticEquationTester {
    
        public static void main(String[] args) {
    
            Scanner scan = new Scanner(System.in);
    
            System.out.println("Insert the value a of the following equation: a*x^(2)+b*x+c=0");
            double a = Double.parseDouble(scan.nextLine());
            System.out.println("Insert the value b of the following equation: a*x^(2)+b*x+c=0");
            double b = Double.parseDouble(scan.nextLine());
            System.out.println("Insert the value c of the following equation: a*x^(2)+b*x+c=0");
            double c = Double.parseDouble(scan.nextLine());
            scan.close();
    
            QuadraticEquation equation1 = new QuadraticEquation(a, b, c);
            equation1.getEquation();
    
            System.out.println("Does this equation has solutions? " + equation1.hasSolutions());
            System.out.println("How many solutions does this equation have? " + equation1.howManySolutions());
    
            if (equation1.getSolution1() == Double.NaN && equation1.getSolution2() == Double.NaN) {
                System.out.println("There are no real solutions");
            }
    
            if (equation1.getSolution1() != Double.NaN) {
                System.out.println("The first solution is: " + equation1.getSolution1());
            } else if (equation1.getSolution1() == Double.NaN) {
                System.out.println("The first solution is not real");
            }
    
            if (equation1.getSolution2() != Double.NaN) {
                System.out.println("The second solution is: " + equation1.getSolution2());
            } else if (equation1.getSolution2() == Double.NaN) {
                System.out.println("The second solution is not real");
            }
    
            if (equation1.getSolution1() != Double.NaN && equation1.getSolution2() != Double.NaN) {
                System.out.println(equation1.getAllSolutions());
            }
    
        }
    
    }
    
    class QuadraticEquation {
    
        // I'm initialing the variables
        private double a;
        private double b;
        private double c;
        private double delta;
    
        // This is the basic constructor: it initializes the variables to zero
        public QuadraticEquation() {
            this.a = 0;
            this.b = 0;
            this.c = 0;
        }
    
        public QuadraticEquation(double aCoefficient, double bCoefficient, double cCoefficient) {
            this.a = aCoefficient;
            this.b = bCoefficient;
            this.c = cCoefficient;
    
            this.delta = (Math.pow(this.b, 2)) - 4 * this.a * this.c;
        }
    
        public void getEquation() {
            System.out
                    .println("The equation we're going to solve is " + this.a + "*x^(2)+" + this.b + "*x+" + this.c + "=0");
        }
    
        public double getSolution1() {
            double solution1 = (-this.b + (Math.sqrt(this.delta))) / (2 * this.a);
            return solution1;
        }
    
        public double getSolution2() {
            double solution2 = (-this.b - (Math.sqrt(this.delta))) / (2 * this.a);
            return solution2;
        }
    
        public String getAllSolutions() {
            double solution1 = (-this.b + (Math.sqrt(this.delta))) / (2 * this.a);
            double solution2 = (-this.b - (Math.sqrt(this.delta))) / (2 * this.a);
            return "The first solution is: " + solution1 + " while the second solution is: " + solution2;
        }
    
        public boolean hasSolutions() {
    
            if (this.delta >= 0) {
                return true;
            } else if (this.delta < 0) {
                return false;
            }
    
            return false;
        }
    
        public String howManySolutions() {
    
            if (this.a == 0 && this.b == 0 && this.c != 0) {
                return "0 Solutions";
            } else if (this.a == 0 && this.b != 0) {
                return "1 Solution";
            } else if (this.a == 0 && this.b == 0 && this.c == 0) {
                return "Infinite Solutions";
            } else {
                return "2 Solutions";
            }
    
        }
        
        public static boolean isNaN(double v) {
            if (v == Double.NaN) {
                return true;
            }
            else return false;
            
        }
    
    }

【问题讨论】:

标签: java equation-solving


【解决方案1】:

您应该使用Double.isNaN(x) 而不是比较x == Double.NaN

NaN != NaN 是 NaN 的一个有趣属性。具体原因可以查看this question

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 2021-06-10
    • 2018-07-21
    • 2016-01-19
    • 2021-06-28
    • 1970-01-01
    • 2021-12-17
    • 2016-02-29
    相关资源
    最近更新 更多