【发布时间】:2019-11-14 18:07:34
【问题描述】:
我为混乱的代码道歉,但这是我们应该使用的两种方法。我必须在给定的字符串中找到单词end,如果字符串中没有结尾,则返回一个空字符串。 ex (the, end 表示作者) output= the end
public class end {
/**
* find first apperance of the word end.
* return the string up to word end
* return empty string if the word end isn't there
*/
public static int endArray(String[] words) {
int end = 0;
for (int i = 0; i < words.length; i ++) {
end ++;
if (words[i] == "end") {
end++;
} else {
end = -1;
}
}
}
public static String end(String[] words) {
String end = "-1";
for (int i = 0; i < words.length; i ++) {
end+= words[i];
if (words[i] == "end") {
System.out.println(words[i-1]);
end++;
}
}
return end;
}
}
【问题讨论】:
-
我无法弄清楚为什么给定字符串
(the, end said the author)的输出是the end。你是怎么把字符串拆分成字符串数组的?
标签: java arrays string if-statement return-type