Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 捕获异常后for循环继续出现问题_Java_For Loop_Exception Handling_Inputmismatchexception - Fatal编程技术网

Java 捕获异常后for循环继续出现问题

Java 捕获异常后for循环继续出现问题,java,for-loop,exception-handling,inputmismatchexception,Java,For Loop,Exception Handling,Inputmismatchexception,嗨,我是java的新手,我想不出这一点。捕获异常后,我希望for循环继续并继续读取新的整数。这是一个网上的挑战,希望你能接受 5(这说明了之后应该期望多少输入) 并转化为这个输出: -150 can be fitted in: * short * int * long 150000 can be fitted in: * int * long 1500000000 can be fitted in: * int * long 21333333333333333333333333333333333

嗨,我是java的新手,我想不出这一点。捕获异常后,我希望for循环继续并继续读取新的整数。这是一个网上的挑战,希望你能接受 5(这说明了之后应该期望多少输入)

并转化为这个输出:

-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
我想让计算机检查一个数字对于一个字节、短字节、int字节和长字节来说是否不会太大。在达到21333333333333333之前,它一直有效(可能不是最好的方式)。它会导致InputMismatchException(bc its太大),代码会捕获它,但之后它就不工作了。以下是输出:

 -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.");
        }   
    }
}
publicstaticvoidmain(字符串[]args){
int numofinput=0;
扫描仪扫描=新扫描仪(System.in);
numofinput=scan.nextInt();
int[]输入;
输入=新的整数[numofinput];
int i=0;
对于(i=i;i如果(input[i]>=-127&&input[i]=-32768)&&(input[i]=-2147483648)&&(input[i]=-9223372036854775808L)&&(input[i]异常发生后,不匹配的输入在
扫描器
中仍然无人认领,因此您将永远在循环中捕获相同的异常

要解决此问题,您的程序需要从
扫描仪
中删除一些输入,例如在
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
的条件

if((input[i] >=-9223372036854775808L) && (input[i] <=9223372036854775807L))
而不是

if((input[i] >=-2147483648) && (input[i] <=2147483647))

if((input[i]>=-2147483648)和&(input[i]如果nextInt方法正在抛出异常,那么输入是否已被声明?他的输出将表明@BenKnoble我认为OP截断了他的输出。
nextInt
将数据留在
Scanner
中,如果它无法处理().好的,很公平。谢谢!您好,谢谢您的快速回复,但仍然不起作用。我收到了与以前相同的输出。@JTattack我对您的代码进行了两次更改,效果很好。请参阅演示。
if((input[i] >= Integer.MIN_VALUE) && (input[i] <= Integer.MAX_VALUE))
if((input[i] >=-2147483648) && (input[i] <=2147483647))