Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Pig拉丁语get(ranslatetay)代替(anslatetray)_Java - Fatal编程技术网

Java Pig拉丁语get(ranslatetay)代替(anslatetray)

Java Pig拉丁语get(ranslatetay)代替(anslatetray),java,Java,如何使indexOfVowel方法为translate等字符串返回1?我需要它来找出元音前的所有辅音,然后移到末尾。如果我把“not元音”改为2,“translate”这个词应该是“anslatetray”,但它返回ranslatetay。它只检查第一个字母,不检查单词的其余部分 private String translateWord(String word) { String translated = ""; int vowel = 0;

如何使indexOfVowel方法为translate等字符串返回1?我需要它来找出元音前的所有辅音,然后移到末尾。如果我把“not元音”改为2,“translate”这个词应该是“anslatetray”,但它返回ranslatetay。它只检查第一个字母,不检查单词的其余部分

    private String translateWord(String word) {
    String translated = "";
    int vowel = 0;
    int notVowel = 1;

    if (vowel == indexOfVowel(word)) {
        return (word + "way ");
    }
    if (notVowel == indexOfVowel(word)) {
        return (word.substring(1) + word.substring(vowel, 1) + "ay ");
    }
    return translated;
}
private static int indexOfVowel(String word) {
    int index = 0;
    for (int i = 0; i < word.length(); i++) {
        if (isVowel(word.charAt(i))) {
            return i;
        }
        } 
        return word.length();
      
        }
private static boolean isVowel(char ch) {
    switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
        default:
            return false;
    }
}
private String translateWord(字符串字){
字符串翻译为“”;
int元音=0;
int-not元音=1;
如果(元音==索引词(单词)){
返回(单词+方式);
}
if(非元音==索引词(单词)){
return(word.substring(1)+word.substring(元音,1)+“ay”);
}
返回翻译;
}
私有静态int indexOfVowel(字符串字){
int指数=0;
for(int i=0;i

}我认为您不希望
indexofowel
在任何情况下都返回1。你想让它做它听起来会做的事情…返回传递给它的单词中第一个元音的索引。如果第一个元音恰好位于单词的开头,它将返回0,否则它将返回一个非0值,指示第一个元音的位置。你需要那个位置在别处做正确的事情。因此,
indexOfVowel
是正确的

你的问题是你如何使用
子串
方法…理解如何使用它来挑选目标词的正确部分

下面是代码的一个修改版本,它做了正确的事情,带有注释来解释
子字符串的两种用法:

public class Test {

    private static String translateWord(String word) {
        int i = indexOfVowel(word);
        if (i == 0) { // if word starts with vowel
            return (word + "way ");
        }
        else { // word doesn't start with a vowel
            // 'i' is the position of the first vowel
            // word.substring(i) = from position of first vowel to end of word
            // word.substring(0, i) = from start of word to char before first vowel
            return (word.substring(i) + word.substring(0, i) + "ay ");
        }
    }

    private static int indexOfVowel(String word) {
        for (int i = 0; i < word.length(); i++) {
            if (isVowel(word.charAt(i))) {
                return i;
            }
        }
        return word.length();

    }
    private static boolean isVowel(char ch) {
        switch (ch) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                return true;
            default:
                return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(translateWord("action"));
        System.out.println(translateWord("translate"));
        System.out.println(translateWord("parachute"));
        System.out.println(translateWord("scrap"));
    }
}
actionway 
anslatetray 
arachutepay 
apscray