在Java函数中继续语句

bou*_*ppo 1 java iteration algorithm continue

我想创建逻辑,使得:if s2为null,调试器会跳过所有复杂的字符串操作并返回null,而不是s1 + s2 + s3在第一个if块中看到.我错了吗?

public static String helloWorld(String s1, String s2, String s3){
   if(s2==null){
     continue;
     return null;
   }

   ... lots of string manipulation involving s1, s2 and s3.

   return (s1+s2+s3);
}
Run Code Online (Sandbox Code Playgroud)

F. *_*ral 6

不要在那里继续使用,继续是for循环,比如

for(Foo foo : foolist){
    if (foo==null){
        continue;// with this the "for loop" will skip, and get the next element in the
                 // list, in other words, it will execute the next loop,
                 //ignoring the rest of the current loop
    }
    foo.dosomething();
    foo.dosomethingElse();
}
Run Code Online (Sandbox Code Playgroud)

做就是了:

public static String helloWorld(String s1, String s2, String s3){
   if(s2==null){
     return null;
   }

   ... lots of string manipulation involving s1, s2 and s3.

   return (s1+s2+s3);
}
Run Code Online (Sandbox Code Playgroud)