【问题标题】:How do I loop back to the start of the TRY statement? [duplicate]如何循环回到 TRY 语句的开头? [复制]
【发布时间】:2013-10-26 20:55:54
【问题描述】:

如何让它再次循环回到 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 重新输入的情况下继续输入值过程?

【问题讨论】:

  • while 循环包裹try-catch 块,也许?
  • 您的意思是要重新开始整个循环(i 为 0),还是继续循环的下一次迭代?

标签: java


【解决方案1】:

您必须将 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

【讨论】:

  • 绝对多余,try- catch 在 for 循环中,否?
  • 没什么多余的。这就是抛出异常的地方(来自 nextInt())。如果你在 catch 块中减少 i,for 循环不会终止(并且索引不会增加),直到你插入了有效的整数。
  • @sᴜʀᴇsʜᴀᴛᴛᴀ 不是。
  • 也许留下评论,为什么不在 for 循环中初始化 Scanner 是一个非常好的主意,而是在之前进行初始化(正如您正确完成的那样)。对OP进行更多教育是件好事。顺便说一句,我 +1。
【解决方案2】:

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

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
}

【讨论】:

  • 不编译。第一行 False 需要是 false。而while(isValide == false) 应该是while(isValid == false)
  • 所有java关键字都是小写,False应该是false
【解决方案3】:

你说如果发生异常如何回滚?不能在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 ");
    }
}

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

【讨论】:

    【解决方案4】:

    如果出现异常会重新开始。

        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--;
                }
            }
    

    【讨论】:

    • 快到了。你为什么不看看我的解决方案。
    【解决方案5】:

    尝试使用以下代码。

    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 ");
    
            }
    

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

    【讨论】:

    • 我想要的是循环回来告诉用户再次输入一个整数。不要忘记用户在错误发生之前输入的值
    【解决方案6】:
    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;
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2012-08-27
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多