本文介绍了如何在字符串中找到元音,并在屏幕上打印出元音最多的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户输入的字符串中找到所有的元音,然后在屏幕上打印出最多的元音。

程序使用用户输入。

用户在小写字母串中输入。

eg

它应该读出:

我尝试将字符串拆分成不同的单词,并且工作。

我只是不知道如何应用for循环来搜索不同的单词。
我需要在方法中工作,所以这是我用来在字符串中查找元音的方法:

  public void findvowel(){
for(int index = 0; index< word.length(); index ++){
char vowel = word.charAt(index); (元音=='a')||
(vowel =='e')||
(元音=='i')||
(元音=='o')||
(vowel =='u')){
System.out.println(vowel);





但是我知道这不是'工作。
您可以帮助我吗?

解决方案
  public class MaxVowels {
public static void main(String [] args){
String sentence =This is a loooooooooong sentence;
int maxVowelCount = 0;
String wordsWithMostVowels = null;
String [] words = sentence.split();

for(String word:words){
int vowelCount = 0;
word = word.toLowerCase();
for(int i = 0; i< word.length(); i ++){
char x = word.charAt(i);
if(x =='a'|| x =='e'|| x =='i'||
x =='o'|| x =='u'){
vowelCount ++;


if(vowelCount> maxVowelCount){
maxVowelCount = vowelCount;
wordsWithMostVowels =单词;

$ b System.out.println(大多数元音字是:+ wordsWithMostVowels);






代码相当简单,不需要解释=)

这段代码忽略了两个单词的元音数量相同的情况。在这种情况下,第一个单词将被用作大多数元音字。


I need to find all vowels in a user inputted string, and then print the word with the most vowels on the screen.
The program uses user input.
The user types in a string of words in lowercase.
e.g.

and it should read out:

I tried splitting the string into different words, and that works.
I only don't know how to apply a "for" loop to search in the different words. I need to work in methods, so this is the method I used to find vowels in the string:

public void findvowel(){
    for(int index = 0;index < word.length();index++){
    char vowel = word.charAt(index);
    if( (vowel == 'a')||
        (vowel == 'e')||
        (vowel == 'i')||
        (vowel == 'o')||
        (vowel == 'u')){
            System.out.println(vowel);
            }
        }
    }

But I know this doesn't work. Can you people help me?

解决方案
public class MaxVowels {
    public static void main(String[] args) {
        String sentence = "This is a loooooooooong sentence";
        int maxVowelCount = 0;
        String wordsWithMostVowels = null;
        String[] words = sentence.split(" ");

        for(String word : words){
            int vowelCount = 0;
            word = word.toLowerCase();
            for(int i = 0; i < word.length() ; i++){
                char x = word.charAt(i);
                if(x == 'a' || x == 'e' || x == 'i' ||
                   x == 'o' || x == 'u'){
                    vowelCount++;
                }
            }
            if(vowelCount > maxVowelCount){
                maxVowelCount = vowelCount;
                wordsWithMostVowels = word;
            }
        }
        System.out.println("Word with most vowels is: " + wordsWithMostVowels);
    }
}  

The code is fairly straightforward and needs no explanation =)
The code ignores the case where two words have the same number of vowels. In this case, the first word will be used as the word with most vowels.

这篇关于如何在字符串中找到元音,并在屏幕上打印出元音最多的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:11