“Pig Latin getting (ranslatetay) instead of (anslatetray)”

huangapple go评论65阅读模式
英文:

Pig Latin getting (ranslatetay) instead of (anslatetray)

问题

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;
    }
}
}
英文:

How do I make the method indexOfVowel to return 1 for strings such as translate? I need it to find all the consonant that is before a vowel to be moved to the end. If I changed "notVowel" to 2 the word "translate" should be "anslatetray" but it returns ranslatetay. It only checks the first letter but not the rest of the word.

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

    if (vowel == indexOfVowel(word)) {
        return (word + &quot;way &quot;);
    }
    if (notVowel == indexOfVowel(word)) {
        return (word.substring(1) + word.substring(vowel, 1) + &quot;ay &quot;);
    }
    return translated;
}
private static int indexOfVowel(String word) {
    int index = 0;
    for (int i = 0; i &lt; word.length(); i++) {
        if (isVowel(word.charAt(i))) {
            return i;
        }
        } 
    	return word.length();
      
        }
private static boolean isVowel(char ch) {
    switch (ch) {
        case &#39;a&#39;:
        case &#39;e&#39;:
        case &#39;i&#39;:
        case &#39;o&#39;:
        case &#39;u&#39;:
            return true;
        default:
            return false;
    }
}

}

答案1

得分: 1

我认为你不希望 indexOfVowel 在任何情况下都返回1。你希望它执行其名称所示的操作...返回传递给它的单词中第一个元音的索引。如果第一个元音恰好位于单词开头,它将返回0;否则,它将返回一个非零值,表示第一个元音的位置。你需要该位置来在其他地方正确处理。因此,indexOfVowel 的实现是正确的。

你的问题在于对 substring 方法的使用...如何使用它来选择目标单词的正确部分。

以下是修改后的代码版本,其中包含注释来解释两次使用 substring 的情况:

public class Test {

    private static String translateWord(String word) {
        int i = indexOfVowel(word);
        if (i == 0) { // 如果单词以元音开头
            return (word + "way ");
        }
        else { // 单词不以元音开头
            // 'i' 是第一个元音的位置
            // word.substring(i) = 从第一个元音位置到单词结尾
            // word.substring(0, i) = 从单词开头到第一个元音前一个字符
            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 
英文:

I don't think you want indexOfVowel to return 1 specifically under any case. You want it to do what it sounds like it will do...return the index of the first vowel in the word passed to it. If the first vowel happens to be at the beginning of the word, it will return 0, otherwise it will return a non-0 value indicating the location of the first vowel. You need that location to do the right thing elsewhere. So indexOfVowel is correct as you have it.

Your problem is your use of the substring method...understanding how to use it to pick out the right portions of the target word.

Here's a modified version of your code that does the right thing, with comments to explain the two uses of substring:

public class Test {
private static String translateWord(String word) {
int i = indexOfVowel(word);
if (i == 0) { // if word starts with vowel
return (word + &quot;way &quot;);
}
else { // word doesn&#39;t start with a vowel
// &#39;i&#39; 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) + &quot;ay &quot;);
}
}
private static int indexOfVowel(String word) {
for (int i = 0; i &lt; word.length(); i++) {
if (isVowel(word.charAt(i))) {
return i;
}
}
return word.length();
}
private static boolean isVowel(char ch) {
switch (ch) {
case &#39;a&#39;:
case &#39;e&#39;:
case &#39;i&#39;:
case &#39;o&#39;:
case &#39;u&#39;:
return true;
default:
return false;
}
}
public static void main(String[] args) {
System.out.println(translateWord(&quot;action&quot;));
System.out.println(translateWord(&quot;translate&quot;));
System.out.println(translateWord(&quot;parachute&quot;));
System.out.println(translateWord(&quot;scrap&quot;));
}
}

Result:

actionway 
anslatetray 
arachutepay 
apscray 

huangapple
  • 本文由 发表于 2020年9月17日 13:16:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63931668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定