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");
}
}
}
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");
}
}
}
© 2024 OneMinuteCode. All rights reserved.