1

如何让它再次循环回到 FOR 循环的开头?我曾尝试搜索答案。使用 continue 似乎对我不起作用。

try{

    for (int i = 0; i < Array1.length; i++){
        System.out.println("enter Values ");

        Scanner input = new Scanner(System.in);
        Array1[i]= input.nextInt();
    }
}//try
catch (InputMismatchException e) 
{
    System.out.println("pls enter integers only ");

}//catch

我该如何再次继续该过程?例如

输入值 5 输入值 1 输入值 g 请仅输入整数 ->> 错误异常在这里结束

在此错误显示之后,我如何能够在不从值 1 重新输入的情况下继续输入值过程?

4

6 回答 6

3

您必须将 try/catch 放在 for 循环中(并减少 catch 块中的 i,因此所有索引都将被填充)。

既然人们似乎对这里的基础知识感到困惑,为什么不拥有完整的代码:

Scanner input = new Scanner(System.in);
System.out.println("Enter integer values: ");
for (int i = 0; i < Array1.length; i++){
    try {
        Array1[i]= input.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("Please enter integers only");
        i--;
    }
}
// Now your Array1 is filled with ints
于 2013-10-18T05:49:36.957 回答
1

你说如果发生异常如何回滚?你不能在 for 循环中插入 try/catch 语句吗?

Scanner input = null;

for (int i = 0; i < Array1.length; i++){
    System.out.println("enter Values ");

    input = new Scanner(System.in);
    try {
        Array1[i]= input.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("pls enter integers only ");
    }
}

代码编辑:最好的编程用途是在循环外声明变量

于 2013-10-18T05:52:17.197 回答
1

如果发生任何异常,它将重新开始。

    for(int i=0;i<a.length;i++){
        try{
            System.out.println("enter Values ");
            Scanner input = new Scanner(System.in);
            a[i]=input.nextInt();
        }
        catch(InputMismatchException e){
            System.out.println(e.getMessage());
            i=-1;
        }
    }

下面将尝试获取它触发异常的值:

for(int i=0;i<a.length;i++){
            try{
                System.out.println("enter Values ");
                Scanner input = new Scanner(System.in);
                a[i]=input.nextInt();
            }
            catch(InputMismatchException e){
                System.out.println(e.getMessage());
                i--;
            }
        }
于 2013-10-18T05:54:02.447 回答
1

尝试使用以下代码。

try{
            for (int i = 0; i < Array1.length; i++){
                System.out.println("enter Values ");

                Scanner input = new Scanner(System.in);
                Array1[i]= input.nextInt();
                if(i==Array1.length-1) i=0;
            }
        }//try
        catch (InputMismatchException e) 
        {
            System.out.println("pls enter integers only ");

        }

这将始终执行相同的循环。这是你想要的吗?

于 2013-10-18T06:12:21.433 回答
0
try{
for (int i = 0; i < Array1.length; i++){    
System.out.println("enter Values ");
Scanner input = new Scanner(System.in);    
    Array1[i]= input.nextInt();
}
} 
catch (InputMismatchException e) {System.out.println("pls enter integers only ");
i = 0;
continue;
}

希望能帮助到你。

于 2013-10-18T06:23:59.097 回答
0

如果您想在捕获异常后再次启动整个过程,

boolean isValid = false;

while(isValid == false) {
try{

    for (int i = 0; i < Array1.length; i++){
        System.out.println("enter Values ");
        Scanner input = new Scanner(System.in);
        Array1[i]= input.nextInt();
    }
    isValid = true;
}//try
catch (InputMismatchException e) 
{
    System.out.println("pls enter integers only ");

}//catch
}
于 2013-10-18T05:51:15.403 回答