【发布时间】:2018-07-20 16:28:47
【问题描述】:
我正在尝试计算Strings 的给定List 中笑脸的出现次数。
笑脸的格式为: 或; 用于眼睛,可选鼻子- 或~,以及) 或D 的嘴巴。
import java.util .*;
public class SmileFaces {
public static int countSmileys(List<String> arrow) {
int countF = 0;
for (String x : arrow) {
if (x.charAt(0) == ';' || x.charAt(0) == ':') {
if (x.charAt(1) == '-' || x.charAt(1) == '~') {
if (x.charAt(2) == ')' || x.charAt(2) == 'D') {
countF++;
} else if (x.charAt(1) == ')' || x.charAt(1) == 'D') {
countF++;
}
}
}
}
return countF;
}
}
【问题讨论】:
-
你有问题还是只是在炫耀你的代码?
-
请提供样本输入数据、期望结果和观察结果。
-
如果字符串大小小于 3,这将抛出
IndexOutOfBoundsException;您可能需要为此添加一些验证。另外,也许你应该看看Pattern类;它会更优雅地解决问题。 -
countSmileys(Collections.singletonList("")) -
对不起......它不工作......测试用例......预期结果得到结果