0

我需要一些帮助来解决我在以下代码中遇到的问题。

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。单词将以辅音开头。我遇到的问题是当字符串包含一个特定的辅音,然后是第一个元音和同一个辅音时,它会去掉第二个辅音。

此特定代码不适用于家庭作业,但我会将其应用于家庭作业。

4

1 回答 1

2

我懂了:

替换这一行:

word = word.replace(wordBeg,"");

word = word.replaceFirst(wordBeg,"");

检查它的文档: http: //docs.oracle.com/javase/6/docs/api/java/lang/String.html

于 2013-06-05T16:26:23.927 回答