我需要编写一个返回单词元音数量的代码,我的代码中一直出现错误,要求输入缺少的语句。有解决方案吗? :3
import java.util.*;
public class vowels
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please type your name.");
String name = input.nextLine();
System.out.println("Congratulations, your name has "+
countVowels(name) +" vowels.");
}
public static int countVowels(String str)
{
int count = 0;
for (int i=0; i < str.length(); i++)
{
// char c = str.charAt(i);
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'o' || str.charAt(i) == 'i' || str.charAt(i) == 'u')
count = count + 1;
}
}
}
答案 0 :(得分:2)
正如几条评论所指出的,你错过了一个回复陈述。
您需要返回count
。
public static int countVowels(String str)
{
int count = 0;
for (int i=0; i < str.length(); i++)
{
// char c = str.charAt(i);
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'o' ||
str.charAt(i) == 'i' || str.charAt(i) == 'u')
count = count + 1;
}
return count;
}