第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

創(chuàng)建一種決定單詞元音和諧的方法

創(chuàng)建一種決定單詞元音和諧的方法

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";}我知道,我知道,元音和聲在英語(yǔ)中不存在,但為了這個(gè)例子,讓我們假裝它確實(shí)存在。在high元音“e”和“I”。該deep元音是'A', 'O'和'U'。所有單詞都屬于組high,deep或mixed。例如:如果一個(gè)詞只有high元音,它是一個(gè)high詞(hell、hill、mill、kill 等)如果一個(gè)詞只有deep元音,它是一個(gè)deep詞(劍、握、凳、涼等)如果一個(gè)詞具有來(lái)自兩個(gè)組的元音,則它是一個(gè)mixed詞(mule、mountain、house、choose 等)唯一的問(wèn)題是,我的代碼無(wú)法正常工作。如果一個(gè)詞是 ,它永遠(yuǎn)不會(huì)顯示mixed。如果一個(gè)單詞中甚至有一個(gè)高字母,它就會(huì)顯示為high. 我需要做什么來(lái)修復(fù)它?我的代碼有什么問(wèn)題?
查看完整描述

3 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

如上所述,代碼有兩個(gè)問(wèn)題:

  • 一旦第一次出現(xiàn)任何條件(這就是它正在檢查的所有條件 - 第一次出現(xiàn)) - 您將獲得結(jié)果。

  • 如果您的輸入比您的任何一個(gè)字母數(shù)組都長(zhǎng)(并且確實(shí)如此),您將獲得一個(gè)ArrayIndexOutOfBoundsException.

在這種情況下,最好的辦法是直接檢查元音等價(jià)性,而不是依靠數(shù)組來(lái)存儲(chǔ)它。

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;

}

錯(cuò)誤處理情況——例如當(dāng)用戶在null此方法中輸入或空字符串時(shí)——留給讀者作為練習(xí)。


查看完整回答
反對(duì) 回復(fù) 2021-10-27
?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以輕松做到:


    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;

    }


查看完整回答
反對(duì) 回復(fù) 2021-10-27
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

我做了一些 Jugaad 只是為了讓生活更輕松。我知道這不是一個(gè)好的編碼習(xí)慣


    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;

     }


查看完整回答
反對(duì) 回復(fù) 2021-10-27
  • 3 回答
  • 0 關(guān)注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)