The continue statement is not available at the method's calling.

Asked 2 years ago, Updated 2 years ago, 35 views

If the array element is 3, I would like to skip it using the continue statement, but a compilation error appears and says continue cannot be used outside the loop.Can't I use continue statements for classes other than those that use for statements?

public class Main {

    public static void main(String[]args) {

        checkService aaa = new checkService();

        int hairetu[] = {1,2,3,4,5};

        for(inti=0;i<hairetu.length;i++){
            aaa.checkContuine(i);
        }
    }
}

public class checkService{

    public void checkContuine(inti){

        if(i==3){
            continue;
        }

        if(i==4){
            System.out.println("4"");
        } else {
            System.out.println("Number other than 3 and 4");
        }
    }
}

java

2022-09-30 19:48

1 Answers

Not available.This is probably because the compiler doesn't know if checkService.checkContine is really called in the loop.

If you want to suppress the output when i is 3, simply return is sufficient.

Example:

class Main {
    public static void main(String[]args) {
        checkService aaa = new checkService();
        int hairetu[] = {1,2,3,4,5};

        for(inti=0;i<hairetu.length;i++){
            aaa.checkContuine(i);
        }
    }
}

class checkService{
    public void checkContuine(inti){
        if(i==3)return;

        if(i==4){
            System.out.println("4"");
        } else {
            System.out.println("Number other than 3 and 4");
        }
    }
}


2022-09-30 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.