Java-如何将String转换为piglatin?

时间:2015-10-28 23:24:30

标签: java string character

我正在写一个猪拉丁语代码,如果单词中的第一个字母是辅音,如何让我的程序识别单词中的下一个元音的位置,我会被绊倒。然后它将单词的第一部分(直到第一个元音)移动到单词的末尾并随之打印出来。 (例如trees = eestray)

这是我现在的代码

// word is bringing in the string entered from the user
public static void translate(String word) {

    String wordCap, pigLatin = "";
    char vowels;
    int lowest = 0, tempOne, tempTwo, tempThree, tempFour, tempFive;  

    wordCap = word.toUpperCase();
    vowels = wordCap.charAt(0);

    if (vowels == 'A' || vowels == 'E' || vowels == 'I' || vowels == 'O' || vowels == 'U') {
            word = word + "way";
            System.out.println(word);
        }
        else {
            tempOne = wordCap.indexOf('A', 1);
            if (lowest > tempOne && tempOne != -1) {
                lowest = tempOne;
            }
            tempTwo = wordCap.indexOf('E', 1);
            if (lowest > tempTwo && tempTwo != -1) {
                lowest = tempTwo;
            }
            tempThree = wordCap.indexOf('I', 1);
            if (lowest > tempThree && tempThree != -1) {
                lowest = tempThree;
            }
            tempFour = wordCap.indexOf('O', 1);
            if (lowest > tempFour && tempFour != -1) {
                lowest = tempFour;
            }
            tempFive = wordCap.indexOf('U', 1);
            if (lowest > tempFive && tempFive != -1) {
                lowest = tempFive;
            }




public static char vowel(String word) {
    int start= 0, end= 0;
    char vowels;

    for (int i = 0; i < word.length(); i++) {
        vowels = word.charAt(i);
        if (vowels == 'A' || vowels == 'E' || vowels == 'I' || vowels == 'O' || vowels == 'U') {
            end = i;
            break;
        }
    }
    return (char) end;
}

(在翻译方法中)

for (int i = 0; i<wordCap.length(); i++) {
        if (vowel(wordCap.charAt(i))) {
            vowels = wordCap.charAt(i);
        }

    }

现在的问题是元音方法不适用于方法类型。它说它必须是一个炭?

2 个答案:

答案 0 :(得分:1)

让我试着为你缩短这种方法;)

尝试这样的事情:

private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'};
public static String translate(String word) {
    int start = 0; // start index of word
    int firstVowel = 0;
    int end = word.length(); // end index of word
    for(int i = 0; i < end; i++) { // loop over length of word
        char c = Character.toLowerCase(word.charAt(i)); // char of word at i, lower cased
        if(Arrays.asList(vowels).contains(c)) { // convert vowels to a list so we can use List.contains() convenience method.
            firstVowel = i;
            break; // stop looping
        }
    }
    if(start != firstVowel) { // if start is not equal to firstVowel, we caught a vowel.
        String startString = word.substring(firstVowel, end);
        String endString = word.substring(start, firstVowel) + "ay";
        return startString+endString;
    }
    return word; //couldn't find a vowel, return original
}

这个片段的作用是迭代单词中的每个字符,将第一个元音的索引存储在firstVowel变量中。然后,我们得到从firstVowelend的每个字符;并将其存储在startString中。然后,我们得到从startfirstVowel的每个字符;添加&#34; ay&#34;,并将其存储在endString中。最后,我们将这些字符串连接在一起并返回它们,从而产生所需的输出。

我们可以使用System.out.println(translate("trees"));

对此进行测试

编辑:没有数组,请求:

public static String translate(String word) {
    char a = 'a';
    char e = 'e';
    char i = 'i';
    char o = 'o';
    char u = 'u';

    int start = 0;
    int firstVowel = 0;
    int end = word.length();
    for(int i = 0; i < end; i++) {
        char c = Character.toLowerCase(word.charAt(i));
        if(c == a || c == e || c == i || c == o || c == u) {
            firstVowel = i;
            break;
        }
    }
    if(start != firstVowel) {
        String startString = word.subString(firstVowel, end);
        String endString = word.subString(start, firstVowel) + "ay";
        return startString+endString;
    }
    return word;
}

正如您所看到的,数组缩短了很多东西!

如果您对Arrays.asList().contains()电话感到迂腐,我们可以定义自己的电话:

public static boolean containsChar(char[] lookIn, char lookFor) {
    boolean doesContainChar = false;
    for(char c : lookIn) {
        if(doesContainChar = c == lookFor)
            break;
    }
    return doesContainChar;
}

答案 1 :(得分:0)

您可能希望使用for循环来迭代每个单词的字母,直到找到元音。例如:

String wordCap = word.toUpperCase();
char vowels;

for (int i=0; i<wordCap.length(); i++) {
    if (isVowel(wordCap.charAt(i))) {
        vowels = wordCap.charAt(i);
        break;
    }
}

当然,为了保持示例的简洁,我只使用了isVowel()。你必须像在第一个if语句中那样将它识别为元音(或者自己编写isVowel()方法)。

要修改单词,您还需要声明一个变量来保存元音的索引。可以为此添加上一部分代码,如下所示:

String wordCap = word.toUpperCase();
char vowels;
int vowelIndex;
String newWord;


for (int i=0; i<wordCap.length(); i++) {
    if (isVowel(wordCap.charAt(i))) {
        vowels = wordCap.charAt(i);
        vowelIndex = i;
        break;
    }
}

然后,您可以在修改单词时引用vowelIndex

if (vowelIndex == 0) {
    newWord = word + "way";
} else {
    newWord = word.substring(vowelIndex) + word.substring(0, vowelIndex) + "ay";
}
return word;