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

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

在java中打印元音數(shù)量最多的單詞

在java中打印元音數(shù)量最多的單詞

素胚勾勒不出你 2023-06-28 15:22:21
我想打印包含最大元音數(shù)的單詞。但問題是包含最大數(shù)量的句子的最后一個(gè)單詞沒有打印。請(qǐng)幫我解決這個(gè)問題。我的代碼如下。當(dāng)我輸入輸入時(shí)'Happy New Year',輸出是 'Yea'。但我想要輸出是'Year'import java.util.Scanner;public class Abcd {    public static void main(String args[]) {        Scanner sc = new Scanner(System.in);        System.out.print("Enter The Word : ");        String sentence = sc.nextLine();        String word = "";        String wordMostVowel = "";        int temp = 0;        int vowelCount = 0;        char ch;        for (int i = 0; i < sentence.length(); i++) {            ch = sentence.charAt(i);            if (ch != ' ' && i != (sentence.length() - 1)) {                word += ch;                  ch = Character.toLowerCase(ch);                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {                    vowelCount++;                 }            } else {                 if (vowelCount > temp) {                    temp = vowelCount;                    wordMostVowel = word;                }                word = "";                vowelCount = 0;            }            }        System.out.println("The word with the most vowels (" + temp + ") is: " + " " + wordMostVowel);    }}
查看完整描述

1 回答

?
慕田峪9158850

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

您可以在空格處剪切單詞(正確),但也可以在最后一個(gè)字符處剪切,即使它不是空格(因此永遠(yuǎn)不會(huì)處理該字符)。這是不正確的。


這是一種可能性:


import java.util.Scanner;


public class Abcd {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the sentence : ");

        String sentence = sc.nextLine();

        String wordMostVowels = "";

        int maxVowelCount = 0;


        for (String word : sentence.split(" ")) {

            int vowelCount = 0;

            for (char c : word.toLowerCase().toCharArray()) {

                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {

                    vowelCount++;

                }

            }


            if (vowelCount > maxVowelCount) {

                maxVowelCount = vowelCount;

                wordMostVowels = word;

            }

        }


        System.out.println("The word with the most vowels (" + maxVowelCount + ") is: " + wordMostVowels);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-28
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(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)