Java 如何循环回TRY语句的开头?

Java 如何循环回TRY语句的开头?,java,Java,如何使它再次回到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 (In

如何使它再次回到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
试试看{
for(int i=0;i
我该如何继续这个过程呢?比如说

输入值 5. 输入值 1. 输入值 G 请只输入整数->>这里有错误例外


显示此错误后,我如何能够继续输入值过程而不从值1重新键入

您必须将try/catch放在for循环中(并在catch块中减少i,以便填充所有索引)

既然人们似乎对这里的基本知识感到困惑,为什么不使用完整的fricking代码:

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
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入整数值:”);
for(int i=0;i
您必须将try/catch放在for循环中(并在catch块中减少i,以便填充所有索引)

既然人们似乎对这里的基本知识感到困惑,为什么不使用完整的fricking代码:

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
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入整数值:”);
for(int i=0;i
如果要在捕获异常后再次启动整个过程

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
}
boolean isValid=false;
while(isValid==false){
试一试{
for(int i=0;i
如果要在捕获异常后再次启动整个过程

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
}
boolean isValid=false;
while(isValid==false){
试一试{
for(int i=0;i
您说如果发生异常,如何回滚?不能在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 ");
    }
}
扫描仪输入=null;
for(int i=0;i

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

你说如果发生异常,如何回滚?不能在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 ");
    }
}
扫描仪输入=null;
for(int i=0;i

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

如果出现任何异常,它将重新启动。

    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如果出现任何异常,它将重新启动。

    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(inti=0;i尝试使用以下代码

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

        }
试试看{
for(int i=0;i

它将始终如一地执行相同的循环。这是您想要的吗?

尝试使用以下代码

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

        }
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;
}
试试看{
for(int i=0;i
它将始终如一地执行相同的循环。这是您想要的吗?

试试看{
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;
}
对于(int i=0;i
希望有帮助。

试试看{
对于(int i=0;i

希望能有所帮助。

也许可以用
while
循环来包装
try catch
块?你的意思是想再次开始整个循环(i为0),还是继续循环的下一次迭代?也许可以用
while
循环来包装
try catch
块?可以吗