當我運行此代碼並使用大於long範圍的值時,則輸出爲「0無法在任何位置安裝」。 我想輸出: 「×(我所給定的輸入這是很長的範圍之外)不能在任何地方安裝」打印一個超出範圍的數字
import java.util.*;
import java.io.*;
class Solution {
public static void main(String[] argh) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
long x = 0;
try {
x = sc.nextLong();
System.out.println(x + " can be fitted in:");
if (x >= -128 && x <= 127)
System.out.println("* byte");
if (x >= -32768 && x <= 32767)
System.out.println("* short");
if (x >= -2147483648 && x <= 2147483647)
System.out.println("* int");
if (x >= -9223372036854775808l && x <= 9223372036854775807l)
System.out.println("* long");
// Complete the code
} catch (Exception e) {
System.out.println(x + " can't be fitted anywhere.");
}
}
}
}
不要忘了在最後關閉'Scanner':sc.close();'。 –