Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在For循环内部的If语句中继续_Java_Android - Fatal编程技术网

Java 在For循环内部的If语句中继续

Java 在For循环内部的If语句中继续,java,android,Java,Android,我有一个for循环,需要跳过一些行 简单地说,我是这样做的: for(int x=0;x

我有一个for循环,需要跳过一些行

简单地说,我是这样做的:

for(int x=0;x<6;x++){
如果(x==3){
继续;
}
Log.i(“LOGLOG”、“LOGLOG”);
}
continue语句是否会像跳转到for循环的另一个迭代中那样工作?如果没有,最好的方法是什么?或者我如何优化这个


提前感谢。

是,继续将影响for循环。您将跳过当前循环块中的其余代码,并开始下一次迭代

中断和继续不影响if语句,它们只影响循环(以及开关的中断)

如果需要跳转几个循环,甚至可以使用

class ContinueWithLabelDemo {
    public static void main(String[] args) {

        String searchMe = "Look for a substring in me";
        String substring = "sub";
        boolean foundIt = false;

        int max = searchMe.length() - 
                  substring.length();

    test:
        for (int i = 0; i <= max; i++) {
            int n = substring.length();
            int j = i;
            int k = 0;
            while (n-- != 0) {
                if (searchMe.charAt(j++) != substring.charAt(k++)) {
                    continue test;
                }
            }
            foundIt = true;
                break test;
        }
        System.out.println(foundIt ? "Found it" : "Didn't find it");
    }
}
class ContinueWithLabelDemo{
公共静态void main(字符串[]args){
String searchMe=“查找我的子字符串”;
字符串substring=“sub”;
布尔foundIt=false;
int max=searchMe.length()
substring.length();
测试:

对于(int i=0;i如果您希望显式,您可以这样标记您的循环:

myLoop : for (int x=0; x<6; x++){
        if (x==3){
            continue myLoop;
        }
        Log.i("LOGLOG","LOGLOGLOG");
    }

myLoop:for(int x=0;x有什么要求?当x为3时跳过日志。