简体   繁体   English

在java中检查原始数据类型的输入

[英]checking the input for primitive data types in java

I was given to code a program that would take input of a number and check, in which all primitive data types(byte, short, int, long) it will fit in. I wrote the following code but it is not passing all the test cases:我被要求编写一个程序,该程序将输入一个数字并进行检查,其中所有原始数据类型(字节、短、整数、长)都适合。我编写了以下代码,但没有通过所有测试案例:

import java.util.*;
import java.io.*;


class Solution {
    public static void main(String []args) {
        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();

        for(int i=0;i<t;i++) {

            try {
                long x=sc.nextLong();
                System.out.println(x+" can be fitted in:");
                if(x>=-Byte.MAX_VALUE && x<=Byte.MAX_VALUE)
                    System.out.println("* byte");
                if(x>=-Short.MAX_VALUE && x<=Short.MAX_VALUE)
                    System.out.println("* short");
                if(x>=-Integer.MAX_VALUE && x<=Integer.MAX_VALUE)
                    System.out.println("* int");
                if(x>=-Long.MAX_VALUE && x<=Long.MAX_VALUE)
                    System.out.println("* long");                
            }
            catch(Exception e) {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
        }
    }
}

Of course it'll fail.当然会失败。 Take a byte .取一个byte In Java, it is represented as a signed 8bit value (2's complement notation).在 Java 中,它表示为一个有符号的 8 位值(2 的补码表示法)。 It means its range varies from -128 to +127.这意味着它的范围从 -128 到 +127。 You logic says that it'll be a byte if it lies b/w (inclusive of both sides), -127 to +127.您的逻辑说,如果它位于黑白(包括两侧),-127 到 +127,它将是一个字节。 It'll fail for -128.它会失败 -128。

Replace -Byte.MAX_VALUE with Byte.MIN_VALUE and it'll work.更换-Byte.MAX_VALUEByte.MIN_VALUE ,它会工作。

You should be using if ... else logic here, to prevent a given input from matching more than one case:您应该在这里使用if ... else逻辑,以防止给定输入匹配多个案例:

long x = sc.nextLong();
System.out.println(x+" can be fitted in:");
if (x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) {
    System.out.println("* byte");
}
else if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) {
    System.out.println("* short");
}
else if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) {
    System.out.println("* int");
}
else if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) {
    System.out.println("* long");
}
else {
    System.out.println("value does not fit any type");
}

I hope if you give a value which can't be taken by a long variable, the value will wrap around between Long.MAX_VALUE & Long.MIN_VALUE.我希望如果你给出一个不能被 long 变量采用的值,该值将在 Long.MAX_VALUE 和 Long.MIN_VALUE 之间环绕。 What i can suggest is, get the input as a string from the user and construct BigDecimal and compare the range values of the primitives with the BigDecimal Values using some builtin methods.我可以建议的是,从用户获取输入作为字符串并构造 BigDecimal 并使用一些内置方法将基元的范围值与 BigDecimal 值进行比较。

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

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