猪英语翻译程序显示不正确

时间:2018-12-16 01:31:44

标签: java

我正在尝试创建一个程序,将英语转换为Pig English。 目前一切正常,除非我要在第一个字母,第二个字母或第三个字母中搜索辅音以找到下一个元音,然后将这些辅音移到单词的末尾并添加“ ay”。

如下面的输出示例所示,此代码识别出“ e”是单词“ remember”的第二个字母中的元音,并将其更改为“ emember-ray”。 然后,我尝试使用“马铃薯”,希望它识别出“ o”是单词的第二个字母。 奇怪的是,它将“土豆”转换为“土豆”,而不是“ otato-pay” 任何想法为什么会发生这种情况?我确定,这与for循环中的else if语句有关。

代码如下:

class Main {

    public static String engPig(String englishWord)
    {
        String pigWord = englishWord;

        String[] vowel = {
                "a", "e", "i", "o", "u"
        };
        String removeQAndU = "";
        String consonants = "";

        for(int i = 0; i < vowel.length; i++) {     
            if(pigWord.startsWith(vowel[i])) { // If the first letter of the word is a vowel then keep it the same and add -ay.
                pigWord = pigWord + "-ay";
            }
            else
            {
                if(pigWord.startsWith("qu")) {
                    removeQAndU = pigWord.substring(2); // Saves the rest of the word (starting at third letter - substring 2) in a variable. Substring index also starts at 0 for first letter, 1 for second letter etc. First two letters "qu" are now so far removed from the word.
                    pigWord = removeQAndU + "-quay"; // Then add quay at the end
                    break;
                }
                else if(!pigWord.startsWith(vowel[i])) {
                    if(pigWord.substring(1, 2).equals(vowel[i]) && !pigWord.substring(2, 3).equals(vowel[i])) {
                    consonants = pigWord.substring(0, 1);
                    pigWord = pigWord.substring(1) + "-" + consonants + "ay";
                    break;
                }
                else if(!pigWord.substring(1, 2).equals(vowel[i]) && pigWord.substring(2, 3).equals(vowel[i])) {
                    consonants = pigWord.substring(0, 2);
                    pigWord = pigWord.substring(2) + "-" + consonants + "ay";
                    break;
                }
                else if(!pigWord.substring(1, 2).equals(vowel[i]) && !pigWord.substring(2, 3).equals(vowel[i]) && pigWord.substring(3, 4).equals(vowel[i])) {
                    consonants = pigWord.substring(0, 3);
                    pigWord = pigWord.substring(3) + "-" + consonants + "ay";
                    break;
                }
            }
        }
    }
    //You will build pigWord using substrings of englishWord
    //Do not edit below this point
    return pigWord;
}

  public static void main(String[] args) {
    System.out.println("Computer Programming 1 Fall Final Program");
    PLS.run();
}
}

输出:

  

计算机编程1年级最终程序   
欢迎使用猪拉丁翻译机!   
输入要翻译的句子(不标点):学校   
ool-schay   
您想翻译另一个吗? ÿ   
输入要翻译的句子(不带标点符号):记住   
emember-ray   
您想翻译另一个吗? ÿ   
输入要翻译的句子(不标点):马铃薯   
ato-potay   
您想翻译另一个吗?

1 个答案:

答案 0 :(得分:0)

对于Potato!pigWord.substring(1, 2).equals(vowel[i]) && !pigWord.substring(2, 3).equals(vowel[i]) && pigWord.substring(3, 4).equals(vowel[i])成立。

您将vowel数组作为{"a", "e", "i", "o", "u"}。因此,循环从i = 0开始并检查pigWord.startsWith(vowel[i])。是错的。

因此,涉及else条件。

当前为pigWord.substring(1, 2) = opigWord.substring(2, 3) = tpigWord.substring(3, 4) = avowel[i] = a。因此,在这种情况下,您的第三个条件将成立。

编辑:

engPig函数的简化版本可以是以下形式(使用Java 8 Stream):

public static String engPig(String englishWord)
{
    String pigWord = englishWord;
    String[] vowel = { "a", "e", "i", "o", "u"};

    if (pigWord.startsWith("qu")) {
        pigWord = pigWord.substring(2) + "-quay";
    } else {
        String pigWordLowerCase = pigWord.toLowerCase();

        Optional<Integer> firstIndex = Arrays.asList(vowel).stream()
                            .map(v -> pigWordLowerCase.indexOf(v))
                            .filter(val -> !val.equals(-1))
                            .min(Comparator.comparingInt(a -> a));

        if (firstIndex.isPresent()) {
            pigWord = pigWord.substring(firstIndex.get()) + "-" + pigWord.substring(0, firstIndex.get()) + "ay";
        }
    }

    return pigWord;
}