我正在製作一個方法presentParticiple,返回一個單詞EnglishWord的現在分詞。當我println時,Char不會添加到我的字符串中?
當我運行程序並且英語字符以輔音結尾時(第2個if語句),我聲明的字符不會被添加。我做錯了什麼?
private String ending1 = ".*[b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z]e";
private String consonant = ".*[b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z]";
public void run() {
presentParticiple("jam");
}
private String presentParticiple(String EnglishWord) {
String correctWord = "";
if (EnglishWord.endsWith(ending1)) {
correctWord = EnglishWord.substring(0,EnglishWord.length()-1)+"ing";
}
else if (EnglishWord.endsWith(consonant)) {
char ch = EnglishWord.charAt(EnglishWord.length());
correctWord = EnglishWord.substring(0, EnglishWord.length())+ch+"ing";
} else {
correctWord = EnglishWord+"ing";
}
println(correctWord);
return correctWord;
}
'String'中的最後一個字符的索引爲'length() - 1'。 – Cinnam
Java中的索引大多數情況下從0開始,這意味着最後一個索引將是'lengt - 1'。 – Pshemo
另外'endsWith'不使用正則表達式(和字符類'[...]''不需要像'''這樣的分隔符,所以只需要寫'。* [bcdfg ...]'或者使用交集消除範圍內不需要的字符'az') – Pshemo