为什么Java中的Nan没有给出正确的答案?

时间:2015-10-22 07:56:16

标签: java

我是Java的新手,对于初学者来说,我试图制作这个脚本,它会说一条线的长度是否可行。除了一部分之外的一切都在起作用。如果我输入“a”行的长度,检查一切是否正常,那么就会出错。我读了一些关于isNaN的其他主题,但没有解决我的问题。

P.S。当我输入if(a == false)....然后它理解命令并且没有给我任何错误。

import java.util.Scanner;

public class Length {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.println("length to a line");
        double length= input.nextInt();
        boolean a = Double.isNaN(length);


        if(a == true){
            System.out.println("Please enter a number, not a letter etc");
    }           
        else if (length >= 0){
            System.out.println("You entered length that is not negative, which is good");
            if(length > 0){
                System.out.println("The length of this line is " + length);
            } if(length==0){
                System.out.println("The length is zero, that means that this isn't line, but a dot");
            }
        }

        //////with a letter it desn't give the right value

        else{
            System.out.println("Negative length of a line is not possible");

        }


        input.close();
    }

}

0 个答案:

没有答案