Java 如何在字符串中找到元音,并在屏幕上打印元音最多的单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19986654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find vowels in a string, and print the word with the most vowels on the screen?
提问by user2993612
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.
我需要在用户输入的字符串中找到所有元音,然后在屏幕上打印元音最多的单词。
该程序使用用户输入。
用户输入一串小写的单词。
例如
"I like java programming"
“我喜欢java编程”
and it should read out:
它应该读出:
programming
编程
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:
我尝试将字符串拆分为不同的单词,效果很好。
我只是不知道如何应用“for”循环来搜索不同的单词。我需要在方法中工作,所以这是我用来在字符串中查找元音的方法:
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?
但我知道这行不通。你们能帮我吗?
回答by Elliott Frisch
This should be a good starting point; note that the method name now really says what it does -
这应该是一个很好的起点;请注意,方法名称现在真正说明了它的作用 -
// public static int findvowel(String word) {
public static int getVowelCount(String word) {
int count = 0;
if (word != null) {
word = word.trim().toLowerCase();
}
if (word == null || word.length() < 1) {
return count;
}
for (int index = 0; index < word.length(); index++) {
// That Fred he's a
char fred = word.charAt(index);
if ((fred == 'a') || (fred == 'e')
|| (fred == 'i') || (fred == 'o')
|| (fred == 'u')) {
++count;
}
}
System.out.println("For the word \"" + word
+ "\" there are " + count + " vowels");
return count;
}
回答by Little Child
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.
代码相当简单,无需解释 =)
代码忽略了两个单词具有相同元音数量的情况。在这种情况下,第一个单词将用作元音最多的单词。
回答by hthserhs
You're going in the right direction. Few things:
你正朝着正确的方向前进。几样东西:
You don't need to print vowels. You will be counting number of vowels in all the words. As you will be doing it one word at a time, you want to remember counts for words that came earlier. A better strategy is to remember only the word having maximum number of vowels. Whenever you find a word with more vowels, you update your result.
您不需要打印元音。您将计算所有单词中元音的数量。由于您将一次做一个单词,因此您想记住之前出现的单词的计数。更好的策略是只记住具有最大元音数的单词。每当你发现一个带有更多元音的单词时,你就更新你的结果。
You can use fieldsto remember the word with maximum number of vowels along with the number:
您可以使用字段来记住具有最大元音数的单词以及数字:
String wordWithMaxVowels;
int maxNumberOfVowels;
Suppose at this instance you're working on a single word
. You need a localvariable to keep a count of vowels in this word
.
假设在这种情况下您正在处理单个word
. 您需要一个局部变量来保持 this 中元音的数量word
。
int vowelCount = 0;
// Your code to loop over the word but remember
// to increase vowelCount if you find a vowel:
// vowelCount++;
Finally the check to see if this number is greater than the maximum we have so far. Update the fields if that's the case:
最后检查这个数字是否大于我们目前拥有的最大值。如果是这种情况,请更新字段:
if(vowelCount > maxNumberOfVowels) {
wordWithMaxVowels = word;
maxNumberOfVowels = vowelCount;
}
Another tip follows. To check if a character c
is a vowel, you can:
另一个提示如下。要检查字符c
是否为元音,您可以:
if ("aeiouAEIOU".indexOf(c) != -1) {
vowelCount++;
}
回答by frankie liuzzi
Your findVowel() method is almost there. Why are you outputting the vowels when you're supposed to count them instead? Instead of findVowel(), i think you want something called countVowels() that returns the amount of vowels in a word. Something like this:
您的 findVowel() 方法几乎就在那里。当你应该计算它们时,为什么要输出元音?而不是 findVowel(),我认为你想要一个叫做 countVowels() 的东西,它返回一个单词中元音的数量。像这样的东西:
public int countVowels(String word){
int count = 0;
for(int index = 0;index < word.length();index++){
char vowel = word.charAt(index);
if( (vowel == 'a')||
(vowel == 'e')||
(vowel == 'i')||
(vowel == 'o')||
(vowel == 'u')){
count++;
}
}
return count;
}
this way, you can call countVowels() on every word in the sentence, and keep track of the word with the most vowels so far. Ex:
这样,您可以对句子中的每个单词调用 countVowels(),并跟踪到目前为止元音最多的单词。前任:
String sentence = "This is a sentence.";
String[] words = sentence.split(" "); //splits sentence into string array by spaces
String maxStringSoFar = "";
int maxStringVowelCount = 0;
for(String s : words)
{
int currentWordVowelCount = countVowels(s);
if(currentWordVowelCount > maxStringVowelCount)
{
maxStringSoFar = s;
maxStringVowelCount = currentWordVowelCount;
}
}
回答by Nitika Goswami
Hey I have some Different answer-----
嘿,我有一些不同的答案-----
class strDemo2
{
public static void main(String args[])
{
String s1=new String("The Ghost of The Arabean Sea");
char c1[]=new char[30];
char c2[]={'a','e','i','o','u'};
s1.getChars(0,28,c1,0);
int pf=0,pl=0;
for(int i=0;i<s1.length();i++)
{
for(int j=0;j<5;j++)
{
if(c1[i]==c2[j])
{
System.out.println("vowel found at:-> "+(i+1)+" which is "+c2[j]);
}
}
}
}
}
回答by Harsh Raj
public class Test2{
public static void main(String[] args) {
String message = "words containig mooooost vowels";
String wordWithMostVowel = null;
int maxVowelCount = 0;
String tests[] = message.split(" ");
int totalVowel = 0;
for(String test : tests){
int vowelCount = 0;
test = test.toLowerCase();
for(int i=0;i<test.length();i++){
switch(test.charAt(i)){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount++;
}
if(vowelCount > maxVowelCount){
maxVowelCount = vowelCount;
wordWithMostVowel = test;
}
}
totalVowel = totalVowel+vowelCount;
}
System.out.println("total vowels "+totalVowel+" word with max vowel is "+wordWithMostVowel);
}
}
}
回答by sivananda
public static void Vowels(){
String Name = "seleniumtesting";
String Vowels = "aeiou";
char[] N = Name.toCharArray();
for(int i=0; i<N.length; i++){
if(Vowels.contains(String.valueOf(N[i]))){
System.out.println("It is a Vowel : "+ N[i]);
}
}
}