为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ITMISS 2021-10-27 16:23:27
  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. 我需要做什么来修复它?我的代码有什么问题?
查看完整描述

3 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

如上所述,代码有两个问题:

  • 一旦第一次出现任何条件(这就是它正在检查的所有条件 - 第一次出现) - 您将获得结果。

  • 如果您的输入比您的任何一个字母数组都长(并且确实如此),您将获得一个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此方法中输入或空字符串时——留给读者作为练习。


查看完整回答
反对 回复 2021-10-27
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您可以轻松做到:


    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;

    }


查看完整回答
反对 回复 2021-10-27
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

我做了一些 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;

     }


查看完整回答
反对 回复 2021-10-27
  • 3 回答
  • 0 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号