int查找适当的Java数据类型
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
BigInteger number = new BigInteger(input.nextLine());
int bitLength = number.bitlength();
if (bitLength <= Bytes.SIZE)
System.out.println("\u8211 byte");
if (bitLength <= Short.SIZE)
System.out.println("\u8211 short");
if (bitLength <= Int.SIZE)
System.out.println("\u8211 int");
if (bitLength <= Long.SIZE)
System.out.println("\u8211 long");
if (bitLength > Long.SIZE)
System.out.println(number + " can't be fitted anywhere.");
}
}
任务:找到合适的数据类型 样本输入:5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
样本输出:
-150 can be fitted in:
short
int
long
150000 can be fitted in:
int
long
1500000000 can be fitted in:
int
long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
long
错误1:
error: cannot find symbol
int bitLength = number.bitlength();
^
错误2:
symbol: method bitlength()
location: variable number of type BigInteger
错误3:
error: cannot find symbol
if (bitLength <= Int.SIZE)
^
symbol: variable Int
location: class Solution
# 1 楼答案
# 2 楼答案
逐行读数字。使用
BigInteger
计算位并将其除以8以简化switch
大小写。请查看以下代码:# 3 楼答案
# 4 楼答案
要将此字符放入代码us
\u8211
您需要使用可以容纳和编号的数据类型
试试这个
解决了这个问题后,要想让它正常工作还有很多事情要做,但要使用BigInteger。bitLength()是更优雅的解决方案
Java中没有类型
Int
,它是Integer
# 5 楼答案
您可以简单地将条件与数据类型的范围放在一起,并检查输入编号是否属于哪种数据类型