我想创建逻辑,以便:如果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);
}
答案 0 :(得分: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();
}
只是这样做:
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);
}
答案 1 :(得分:2)
您不需要continue
,return null;
就够了。
continue
。
示例:
for(int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
System.out.print(i + ",");
}
将打印:
0,1,3,4,