简体   繁体   English

Java > int to double(Scanner>System.out)

[英]Java > int to double(Scanner>System.out)

It is my first question) Hope someone can help:这是我的第一个问题)希望有人可以提供帮助:

I'm trying write next programm: So i have 1 input and max 2 output First of all i am checking if input is (int!) if TRUE i print out message that "YES IT IS INTEGER" and show "saved number in varaible int",我正在尝试编写下一个程序:所以我有 1 个输入和最大 2 个输出首先我正在检查输入是否为(整数!)内部",

but if i put double, i want to print out message =(" NO it is not INT) + and show it in double format!但是如果我放双,我想打印出 message =(" NO it is not INT) + 并以双格式显示!

Everything is ok while i put int but when i put double it is not correct.当我输入 int 时一切正常,但是当我输入 double 时它不正确。

public class Scan {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int check;
            System.out.println("Your number? ");

        if(sc.hasNextInt()){
        check=sc.nextInt();
            System.out.println("OKEI "+check+ " IT IS INTEGER! ");}
        else if(sc.hasNextDouble()){
        check=sc.nextDouble();
            System.out.println("Nooo " + check + " NO it is not INT");
            }
    }

}

One solution is having check as a double instead, and change the check to:一种解决方案是将check改为double check ,并将检查更改为:

if (check == Math.floor(check))

In your solution, you're trying to assign a double to an int .在您的解决方案中,您试图为int分配一个double Since it doesn't fit, it'll loose precision and be truncated.由于它不适合,它会失去精度并被截断。

Final solution should look like:最终解决方案应如下所示:

Scanner sc = new Scanner(System.in);
check = sc.nextDouble();
if (check == Math.floor(check)) {
    // an "int"
} else {
    // not integer
}

Create a new double variable and assign sc.nextDouble() to that double variable.创建一个新的 double 变量并将 sc.nextDouble() 分配给该 double 变量。

Example: double checkDouble;示例:double checkDouble; .... ....

else if(sc.hasNextDouble()){
    checkDouble=sc.nextDouble();
        System.out.println("Nooo " + check + " NO it is not INT");
        }

While trying your code in Eclipse, there was en error using check = sc.nextDouble() ( check being an int ), you can fix it like that:在 Eclipse 中尝试您的代码时,使用check = sc.nextDouble() ( check check = sc.nextDouble() an int ) 出现错误,您可以像这样修复它:

  public static void main(final String[] args) {
    System.out.println("Your number? ");
    final Scanner sc = new Scanner(System.in);
    if (sc.hasNextInt()) {
      final int check = sc.nextInt();
      System.out.println("OKEI " + check + " IT IS INTEGER! ");
    } else if (sc.hasNextDouble()) {
      final double check = sc.nextDouble();
      System.out.println("Nooo " + check + " NO it is not INT");
    } else {
      System.out.println("Failed.");
    }
  }

That way, you won't lose any decimal part using Math.floor or a cast.这样,您就不会使用Math.floor或演员表丢失任何小数部分。

Cast int to double value, It may help.将 int 转换为 double 值,这可能会有所帮助。

else if (sc.hasNextDouble()) {
            check = (int) sc.nextDouble();
            System.out.println("Nooo " + check + " NO it is not INT");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM