我正在制作一个方法presentParticiple,它返回一个单词EnglishWord的现在分词。
当我运行程序并且EnglishWord以辅音(第二个if语句)结束时,我声明的char不会被添加。我做错了什么?
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;
}
答案 0 :(得分:0)
以下是一些有效的代码(包含上述建议)。
public class EnglishTest
{
public static void main (String[] args)
{
String[] words = { "jam", "rue", "raze", "knock", "need" };
for (String word : words)
presentParticiple (word);
}
private static final String ending1 = ".*[b-df-hj-np-tv-z]e";
private static final String consonant = ".*[b-df-hj-np-tv-z]";
private static String presentParticiple (String englishWord)
{
String correctWord = "";
if (englishWord.matches (ending1))
correctWord = englishWord.substring (0, englishWord.length () - 1) + "ing";
else if (englishWord.matches (consonant))
{
char ch = englishWord.charAt (englishWord.length () - 1);
correctWord = englishWord + ch + "ing";
}
else
correctWord = englishWord + "ing";
System.out.println (correctWord);
return correctWord;
}
}
你的算法有问题。试着敲打'例如。