【发布时间】:2020-03-21 18:38:12
【问题描述】:
codingbat.com 中存在问题,您应该从原始字符串中删除“yak”子字符串。他们提供了一个解决方案,我无法理解当 if 语句为真时会发生什么!
public String stringYak(String str) {
String result = "";
for (int i=0; i<str.length(); i++) {
// Look for i starting a "yak" -- advance i in that case
if (i+2<str.length() && str.charAt(i)=='y' && str.charAt(i+2)=='k') {
i = i + 2;
} else { // Otherwise do the normal append
result = result + str.charAt(i);
}
}
return result;
}
它只是将 i 加起来 2 和什么?当它附加到结果字符串时?
【问题讨论】:
-
有什么问题?
if语句是true当找到的子字符串是“牦牛”时,它会在i上加 +2 以跳过该单词并继续循环以查找更多牦牛。 -
应该也是检查字母a吧
标签: java if-statement logical-operators