Java程序计算给定句子中辅音的数量
计算给定句子中辅音的数量:
接收用户的句子。
创建一个变量(计数),将其初始化为0;
将句子中的每个字符与字符{'a','e','i','o','u'}进行比较。如果未发生匹配,则增加计数。
最后打印计数。
示例
import java.util.Scanner;
public class CountingConsonants {
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' ){
System.out.print("");
}else if(ch != ' '){
count++;
}
}
System.out.println("给定句子中辅音的数量为 "+count);
}
}输出结果
输入一个句子: Hi hello how are you welcome to nhooo 给定句子中辅音的数量为 21