G V*_*eep 6 java int byte short long-integer
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.");
}
}
Run Code Online (Sandbox Code Playgroud)
任务:找到合适的数据类型示例输入:5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
Run Code Online (Sandbox Code Playgroud)
样本输出:
-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
Run Code Online (Sandbox Code Playgroud)
错误1:
error: cannot find symbol
int bitLength = number.bitlength();
^
Run Code Online (Sandbox Code Playgroud)
错误2:
symbol: method bitlength()
location: variable number of type BigInteger
Run Code Online (Sandbox Code Playgroud)
错误3:
error: cannot find symbol
if (bitLength <= Int.SIZE)
^
symbol: variable Int
location: class Solution
Run Code Online (Sandbox Code Playgroud)
错误:非法字符:每个 If 语句之前的 \8211
将此字符放入您的代码中\u8211
if语句以及如何输入任何数据类型都无法容纳的数字?
您需要使用可以容纳和编号的数据类型。
试试这个吧。
while (input.hasNextLine()) {
BigInteger number = new BigInteger(input.nextLine());
int bitLength = number.bitLength() + 1;
if (bitLength <= Bytes.SIZE)
System.out.println(" \u8211 byte");
if (bitLength <= Short.SIZE)
System.out.println(" \u8211 short");
// more checks.
if (bitLength > Long.SIZE)
// too big.
Run Code Online (Sandbox Code Playgroud)
解决了这个问题后,还有很多工作要做才能使其正常工作,但使用 BigInteger.bitLength() 是更优雅的解决方案。
如果 (bitLength <= Int.SIZE) 则找不到符号
Java 中没有类型Int,它是Integer.
| 归档时间: |
|
| 查看次数: |
1429 次 |
| 最近记录: |