捕获异常后 for 循环继续出现问题
Trouble with for loop continuing after catching an exception
嗨,我是 java 的半新人,无法弄清楚这个问题。捕获异常后,我希望 for 循环继续并继续读取新整数。这是一项在线挑战,希望您接受
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
我想让计算机检查一个数字对于 byte、short、int 和 long 来说是否太大。它工作(可能不是最好的方式)直到它到达 21333333333333333333333333333333333。它导致 InputMismatchException(因为它很大)并且代码捕获它但在它不起作用之后。这是输出:
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
0 can't be fitted anywhere.
0 can't be fitted anywhere.
我真的想不通任何帮助将不胜感激!
public static void main(String[] args) {
int numofinput = 0;
Scanner scan = new Scanner(System.in);
numofinput = scan.nextInt();
int[] input;
input = new int[numofinput];
int i =0;
for(i = i; i < numofinput && scan.hasNext(); i++){
try {
input[i] = scan.nextInt();
System.out.println(input[i] + " can be fitted in:");
if(input[i] >=-127 && input[i] <=127){
System.out.println("* byte");
}if((input[i] >=-32768) && (input[i] <=32767)){
System.out.println("* short");
}if((input[i] >=-2147483648) && (input[i] <=2147483647)){
System.out.println("* int");
}if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L)){
System.out.println("* long");
}
}catch (InputMismatchException e) {
System.out.println(input[i] + " can't be fitted anywhere.");
}
}
}
问题是不匹配的输入在 Scanner
异常之后仍然无人认领,因此您将永远在循环中捕获相同的异常。
要解决此问题,您的程序需要从 Scanner
中删除一些输入,例如通过在 catch
块中调用 nextLine()
:
try {
...
} catch (InputMismatchException e) {
// Use scan.next() to remove data from the Scanner,
// and print it as part of error message:
System.out.println(scan.next() + " can't be fitted anywhere.");
}
input[]
数组可以替换为单个 long input
,因为您从不使用先前迭代的数据;因此,无需将其存储在数组中。
此外,您应该将对 nextInt
的调用替换为对 nextLong
的调用,否则您将无法正确处理大数。
您还应该删除 long
altogether
的条件
if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L))
因为读取 nextLong
已成功完成,因此保证 true
。
最后,应避免在程序中使用 "magic numbers",而应使用相应内置 Java 类 中的预定义常量,例如
if((input[i] >= Integer.MIN_VALUE) && (input[i] <= Integer.MAX_VALUE))
而不是
if((input[i] >=-2147483648) && (input[i] <=2147483647))
嗨,我是 java 的半新人,无法弄清楚这个问题。捕获异常后,我希望 for 循环继续并继续读取新整数。这是一项在线挑战,希望您接受 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
我想让计算机检查一个数字对于 byte、short、int 和 long 来说是否太大。它工作(可能不是最好的方式)直到它到达 21333333333333333333333333333333333。它导致 InputMismatchException(因为它很大)并且代码捕获它但在它不起作用之后。这是输出:
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
0 can't be fitted anywhere.
0 can't be fitted anywhere.
我真的想不通任何帮助将不胜感激!
public static void main(String[] args) {
int numofinput = 0;
Scanner scan = new Scanner(System.in);
numofinput = scan.nextInt();
int[] input;
input = new int[numofinput];
int i =0;
for(i = i; i < numofinput && scan.hasNext(); i++){
try {
input[i] = scan.nextInt();
System.out.println(input[i] + " can be fitted in:");
if(input[i] >=-127 && input[i] <=127){
System.out.println("* byte");
}if((input[i] >=-32768) && (input[i] <=32767)){
System.out.println("* short");
}if((input[i] >=-2147483648) && (input[i] <=2147483647)){
System.out.println("* int");
}if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L)){
System.out.println("* long");
}
}catch (InputMismatchException e) {
System.out.println(input[i] + " can't be fitted anywhere.");
}
}
}
问题是不匹配的输入在 Scanner
异常之后仍然无人认领,因此您将永远在循环中捕获相同的异常。
要解决此问题,您的程序需要从 Scanner
中删除一些输入,例如通过在 catch
块中调用 nextLine()
:
try {
...
} catch (InputMismatchException e) {
// Use scan.next() to remove data from the Scanner,
// and print it as part of error message:
System.out.println(scan.next() + " can't be fitted anywhere.");
}
input[]
数组可以替换为单个 long input
,因为您从不使用先前迭代的数据;因此,无需将其存储在数组中。
此外,您应该将对 nextInt
的调用替换为对 nextLong
的调用,否则您将无法正确处理大数。
您还应该删除 long
altogether
if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L))
因为读取 nextLong
已成功完成,因此保证 true
。
最后,应避免在程序中使用 "magic numbers",而应使用相应内置 Java 类 中的预定义常量,例如
if((input[i] >= Integer.MIN_VALUE) && (input[i] <= Integer.MAX_VALUE))
而不是
if((input[i] >=-2147483648) && (input[i] <=2147483647))