import java.util.Scanner;
/*
* 计算字符串中元音,辅音,数字和空格的数量
*/
public class NumberOfChar {
private int vowel;
private int consonant;
private int number;
private int space;
private String lines = "13c f efffu os we ef sds sdfsd261 4513";
private char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
/*
* 输入你要查找的字符串
*/
public void getInput() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入字符串");
lines = scanner.next();
}
/*
* 计算字符串中元音的个数
*/
public int ifVowel(String lines) {
for (int i = 0; i < lines.length(); i++) {
for (int j = 0; j < vowels.length; j++) {
if (lines.charAt(i) == vowels[j]) {
vowel++;
}
}
}
return vowel;
}
/*
* 计算字符串中辅音的个数
*/
public int ifConsonant(String lines) {
for (int i = 0; i < lines.length(); i++) {
for (int j = 0; j < vowels.length; j++) {
if(lines.charAt(i) >= 'a' && lines.charAt(i) <= 'z' && lines.charAt(i) != vowels[j]) {
consonant++;
}
}
}
return consonant;
}
/*
* 计算字符串中数字的个数
*/
public int ifNumber(String lines) {
for (int i = 0; i < lines.length(); i++) {
for (char j = '0'; j <= '9'; j++) {
if (lines.charAt(i) == j) {
number++;
}
}
}
return number;
}
/*
* 计算字符串中空格的个数
*/
public int ifSpace(String lines) {
for (int i = 0; i < lines.length(); i++) {
if (lines.charAt(i) == ' ') {
space++;
}
}
return space;
}
public static void main(String[] args) {
NumberOfChar numberOfChar = new NumberOfChar();
// numberOfChar.getInput();
System.out.println(numberOfChar.lines + " 字符串中元音的个数为:" + numberOfChar.ifVowel(numberOfChar.lines));
System.out.println(numberOfChar.lines + " 字符串中辅音的个数为:" + numberOfChar.ifConsonant(numberOfChar.lines));
System.out.println(numberOfChar.lines + " 字符串中数字的个数为:" + numberOfChar.ifNumber(numberOfChar.lines));
System.out.println(numberOfChar.lines + " 字符串中空格的个数为:" + numberOfChar.ifSpace(numberOfChar.lines));
}
}
运行结果: