向各位朋友请教一道题目# Java - 爪哇娇娃
m*2
1 楼
Question:
Return true if the given string contains an appearance of "xyz" where the
xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz"
does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
answer:
public boolean xyzThere(String str) {
for(int i = 0; i < str.length() - 2; i++){
if(str.substring(i, i+3).equals("xyz")){
if(i != 0 && str.substring(i-1, i).equals("."))
{}else{
return true;}
}
}
return false;
}
我的问题是,为什么第二个if之后可以有空的statement{},之后应该是与其对应的else
为什么第一个if之后没有statement, 也没有else;
谢谢大家。
Return true if the given string contains an appearance of "xyz" where the
xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz"
does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
answer:
public boolean xyzThere(String str) {
for(int i = 0; i < str.length() - 2; i++){
if(str.substring(i, i+3).equals("xyz")){
if(i != 0 && str.substring(i-1, i).equals("."))
{}else{
return true;}
}
}
return false;
}
我的问题是,为什么第二个if之后可以有空的statement{},之后应该是与其对应的else
为什么第一个if之后没有statement, 也没有else;
谢谢大家。