Java程序计算给定句子中的元音数量
计算给定句子中的元音数量:
阅读用户的句子
创建一个变量(计数),将其初始化为0;
比较句子中的每个字符与字符{'a','e','i','o','u'}
如果发生匹配,则增加计数。
最后打印计数。
示例
import java.util.Scanner;
public class CountingVowels {
public static void main(String args[]){
int count = 0;
System.out.println("输入一个句子:");
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
for (int i=0 ; i<sentence.length(); i++){
char ch = sentence.charAt(i);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' '){
count ++;
}
}
System.out.println("Number of vowels in the given sentence is "+count);
}
}输出结果
输入一个句子: Hi how are you welcome to nhooo Number of vowels in the given sentence is 22
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短