代码之家  ›  专栏  ›  技术社区  ›  lapartman

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

  •  1
  • lapartman  · 技术社区  · 7 年前
        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 高的 , mixed .

    例如:

    • 如果一个词只有 高的 元音,它是 高的
    • 如果一个词只有 元音,它是 深的 文字(剑、握、凳、凉等)
    • 如果一个单词有两组元音,它就是一个单词 混合的 单词(骡子、山、房子、选择等)

    . 如果一个单词中有一个大写字母,它将显示为 高的

    3 回复  |  直到 7 年前
        1
  •  3
  •   mypetlion    7 年前

    上述代码有两个问题:

    • 你会得到你的结果。
    • 如果您的输入比任何一个字母数组都长(而且是这样),您将得到一个 ArrayIndexOutOfBoundsException

    在这种情况下,最好的方法是直接检查元音等价性,而不是依赖数组来存储元音等价性。

    private static boolean hasHighVowel(String input) {
        return input.contains("e") || input.contains("i");
    }
    
    private static boolean hasLowVowel(String input) {
        return input.contains("a") || input.contains("o") || input.contains("u");
    }
    

    那个 用你的方法。也要注意不要马上从方法返回。

     public static String vowelHarmony(String input) {
        String result = "you screwed something up";
    
        if (hasHighVowel(input)) {
            result = "high";
        }
        if (hasLowVowel(input)) {
            result = "deep";
        }
        if (hasHighVowel(input) && hasLowVowel(input)) {
            result = "mixed";
        }
    
        return result;
    }
    

    错误处理案例-例如当用户输入 null

        2
  •  0
  •   Amin    7 年前

    您可以轻松完成:

        enum Harmony {
            Deep, High, Mixed, Nothing
        }
    
        public static Harmony vowelHarmony(String input) {
            boolean canBeHigh = false, canBeDeep = false;
            if (input.contains("a") || input.contains("o") || input.contains("u"))
                canBeDeep = true;
            if (input.contains("e") || input.contains("i"))
                canBeHigh = true;
            if (canBeDeep && canBeHigh)
                return Harmony.Mixed;
            if (canBeDeep)
                return Harmony.Deep;
            if (canBeHigh)
                return Harmony.High;
            return Harmony.Nothing;
        }
    
        3
  •  0
  •   Rahul Ramamoorthy    7 年前

    嗨,我做了一些Jugaad只是为了让生活变得简单。我知道这不是一个好的编码实践

        enum Harmony
        {
            High,Low,Mixed,Screwed
        }
       public static Harmony vowelHarmony(String input) 
       {
    
            String[] high = {"e", "i"};
            String[] deep = {"a", "o", "u"};
            input=input.replaceAll("[ei]", "1");
            input=input.replaceAll("[aou]", "0");
            input=input.replaceAll("[^01]", "");
            if(input.contains("1") && !input.contains("0"))
                return Harmony.High;
            else if(input.contains("1") && !input.contains("0"))
                return Harmony.Low;
            else if(input.contains("1") && !input.contains("0"))
                return Harmony.Mixed;
            else
                return Harmony.Screwed;
         }