创建一种决定单词元音和谐的方法

  public static String vowelHarmony(String input) {


    String[] high = {"e", "i"};

    String[] deep = {"a", "o", "u"};


    for (int i = 0; i < input.length(); i++) {

        if (input.contains(high[i])&&!input.contains(deep[i])){

            return "high";

        }

        else if (input.contains(deep[i])&&!input.contains(high[i])){

            return "deep";

        }

        else if (input.contains(deep[i])&&input.contains(high[i])){

            return "mixed";

        }

    }

    return "you screwed something up";

}

我知道,我知道,元音和声在英语中不存在,但为了这个例子,让我们假装它确实存在。在high元音“e”和“I”。该deep元音是'A', 'O'和'U'。所有单词都属于组high,deep或mixed。

例如:

  • 如果一个词只有high元音,它是一个high词(hell、hill、mill、kill 等)

  • 如果一个词只有deep元音,它是一个deep词(剑、握、凳、凉等)

  • 如果一个词具有来自两个组的元音,则它是一个mixed词(mule、mountain、house、choose 等)

唯一的问题是,我的代码无法正常工作。如果一个词是 ,它永远不会显示mixed。如果一个单词中甚至有一个高字母,它就会显示为high. 我需要做什么来修复它?我的代码有什么问题?


ITMISS
浏览 154回答 3
3回答

慕娘9325324

如上所述,代码有两个问题:一旦第一次出现任何条件(这就是它正在检查的所有条件 - 第一次出现) - 您将获得结果。如果您的输入比您的任何一个字母数组都长(并且确实如此),您将获得一个ArrayIndexOutOfBoundsException.在这种情况下,最好的办法是直接检查元音等价性,而不是依靠数组来存储它。private static boolean hasHighVowel(String input) {&nbsp; &nbsp; return input.contains("e") || input.contains("i");}private static boolean hasLowVowel(String input) {&nbsp; &nbsp; return input.contains("a") || input.contains("o") || input.contains("u");}然后你可以在你的方法中检查它。还要注意不要立即从方法返回。&nbsp;public static String vowelHarmony(String input) {&nbsp; &nbsp; String result = "you screwed something up";&nbsp; &nbsp; if (hasHighVowel(input)) {&nbsp; &nbsp; &nbsp; &nbsp; result = "high";&nbsp; &nbsp; }&nbsp; &nbsp; if (hasLowVowel(input)) {&nbsp; &nbsp; &nbsp; &nbsp; result = "deep";&nbsp; &nbsp; }&nbsp; &nbsp; if (hasHighVowel(input) && hasLowVowel(input)) {&nbsp; &nbsp; &nbsp; &nbsp; result = "mixed";&nbsp; &nbsp; }&nbsp; &nbsp; return result;}错误处理情况——例如当用户在null此方法中输入或空字符串时——留给读者作为练习。

梵蒂冈之花

您可以轻松做到:&nbsp; &nbsp; enum Harmony {&nbsp; &nbsp; &nbsp; &nbsp; Deep, High, Mixed, Nothing&nbsp; &nbsp; }&nbsp; &nbsp; public static Harmony vowelHarmony(String input) {&nbsp; &nbsp; &nbsp; &nbsp; boolean canBeHigh = false, canBeDeep = false;&nbsp; &nbsp; &nbsp; &nbsp; if (input.contains("a") || input.contains("o") || input.contains("u"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canBeDeep = true;&nbsp; &nbsp; &nbsp; &nbsp; if (input.contains("e") || input.contains("i"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canBeHigh = true;&nbsp; &nbsp; &nbsp; &nbsp; if (canBeDeep && canBeHigh)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.Mixed;&nbsp; &nbsp; &nbsp; &nbsp; if (canBeDeep)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.Deep;&nbsp; &nbsp; &nbsp; &nbsp; if (canBeHigh)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.High;&nbsp; &nbsp; &nbsp; &nbsp; return Harmony.Nothing;&nbsp; &nbsp; }

阿波罗的战车

我做了一些 Jugaad 只是为了让生活更轻松。我知道这不是一个好的编码习惯&nbsp; &nbsp; enum Harmony&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; High,Low,Mixed,Screwed&nbsp; &nbsp; }&nbsp; &nbsp;public static Harmony vowelHarmony(String input)&nbsp;&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; String[] high = {"e", "i"};&nbsp; &nbsp; &nbsp; &nbsp; String[] deep = {"a", "o", "u"};&nbsp; &nbsp; &nbsp; &nbsp; input=input.replaceAll("[ei]", "1");&nbsp; &nbsp; &nbsp; &nbsp; input=input.replaceAll("[aou]", "0");&nbsp; &nbsp; &nbsp; &nbsp; input=input.replaceAll("[^01]", "");&nbsp; &nbsp; &nbsp; &nbsp; if(input.contains("1") && !input.contains("0"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.High;&nbsp; &nbsp; &nbsp; &nbsp; else if(input.contains("1") && !input.contains("0"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.Low;&nbsp; &nbsp; &nbsp; &nbsp; else if(input.contains("1") && !input.contains("0"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.Mixed;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Harmony.Screwed;&nbsp; &nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java