我想在以下代码中解决我遇到的问题。
package piglatin;
public class asdg {
public static void main(String[] args) {
String word = "fifteen";
int vowelSpot = findFirstVowelPosition(word);
String wordBeg = "";
String wordEnd = "ay";
String wordNew = "";
System.out.println(vowelSpot);
wordBeg = word.substring(0,vowelSpot-1);
System.out.println(wordBeg);
word = word.replace(wordBeg,"");
System.out.println(word);
wordNew = word + wordBeg + wordEnd;
System.out.println(wordNew);
}
public static int findFirstVowelPosition(String word)
{
int vowelPosition = -1;
word = word.trim();
for(int i=1; i <=word.length(); i++)
{
if ((word.charAt(i-1) == 'a') ||(word.charAt(i-1) == 'A')
||(word.charAt(i-1) == 'e') ||(word.charAt(i-1) == 'E')
||(word.charAt(i-1) == 'i') ||(word.charAt(i-1) == 'I')
||(word.charAt(i-1) == 'o') ||(word.charAt(i-1) == 'O')
||(word.charAt(i-1) == 'u') ||(word.charAt(i-1) == 'U')
||(word.charAt(i-1) == 'y') ||(word.charAt(i-1) == 'Y')){
vowelPosition = i;
return vowelPosition;
}
}
return vowelPosition;
}
}
此代码的目的是取字符串Word并将其放入piglatin中。 Word将以辅音开头。我遇到的问题是当字符串包含一个特定的辅音,然后是第一个元音和同一个辅音时,它将摆脱第二个辅音。
此特定代码不适用于家庭作业,但我会将其应用于家庭作业。
答案 0 :(得分:2)
我知道了:
替换此行:
word = word.replace(wordBeg,"");
与
word = word.replaceFirst(wordBeg,"");
检查其上的文档:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html