Java 字符串统计元音字母和辅音字母程序

编写一个 Java 程序,使用 for 循环、while 循环、函数和 ASCII 值来计算字符串中的元音字母和辅音字母,并提供示例。

Java 使用 for 循环统计字符串中的元音字母和辅音字母程序

在此字符串辅音和元音计数示例中,我们首先使用 for 循环迭代 vowConsStr。在循环内部,我们将每个字符赋值给 ch,即 (ch = vowConsStr.charAt(i)),以保持代码的简洁性。接下来,我们使用了 Else If 语句。 

在 if 条件内,我们检查字符是否等于 a、e、i、o、u、A、E、I、O 和 U。如果为真,我们就增加元音的数量。在 else if 语句中,我们使用 (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’) 来检查该字符是否是字母。如果为真,我们就增加辅音的数量。这里,我们也可以使用 else if (Character.isAlphabetic(ch))。

import java.util.Scanner;

public class CountVowCons1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String vowConsStr;
		int i, vowels, consonants;
		vowels = consonants = 0;
		char ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Vowel, Consonant String =  ");
		vowConsStr = sc.nextLine();
		
		for(i = 0; i < vowConsStr.length(); i++)
		{
			ch = vowConsStr.charAt(i);
			if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
					ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
				vowels++;
			}
			else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){
				consonants++;
			}
		}		
		System.out.println("\nNumber of Vowel Characters     =  " + vowels);
		System.out.println("Number of Consonant Characters =  " + consonants);
	}
}
Java Program to Count Vowels and Consonants in a String using for loop

Java 使用 While 循环统计字符串中的元音字母和辅音字母程序

以下 toLowerCase() 语句将给定的字符串转换为小写。 

String lw_vowConsStr = vowConsStr.toLowerCase();

与检查大写和小写字母相比,我们只比较小写字母,然后计算元音字母和辅音字母的数量。

import java.util.Scanner;

public class CountVowCons2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String vowConsStr;
		int i, vows, cons;
		i = vows = cons = 0;
		char ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String =  ");
		vowConsStr = sc.nextLine();
		
		String lw_vowConsStr = vowConsStr.toLowerCase();
		
		while(i < lw_vowConsStr.length())
		{
			ch = lw_vowConsStr.charAt(i);
			if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
				vows++;
			}
			else if(ch >= 'a' && ch <= 'z') {
				cons++;
			}
			i++;
		}		
		System.out.println("\nNumber of Vowel Characters     =  " + vows);
		System.out.println("Number of Consonant Characters =  " + cons);
	}
}
Please Enter String =  Tutorial gateway

Number of Vowel Characters     =  7
Number of Consonant Characters =  8

使用 ASCII 值

在此程序示例中,我们通过比较 ASCII 值而不是比较字符来完成。

import java.util.Scanner;

public class CountVowCons3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String vowConsStr;
		int i, vows, const;
		vows = const = 0;
		int cp;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String =  ");
		vowConsStr = sc.nextLine();
		
		String up_vowConsStr = vowConsStr.toUpperCase();
		
		for(i = 0; i < up_vowConsStr.length(); i++)
		{
			cp = up_vowConsStr.codePointAt(i);
			if(cp == 65 || cp == 69 || cp == 73 || cp == 79 || cp == 85) {
				vows++;
			}
			else if(cp >= 65 && cp <= 90 || cp >= 97 && cp <= 122)  {
				const++;
			}
		}		
		System.out.println("\nNumber of Vowel Characters     =  " + vows);
		System.out.println("Number of Consonant Characters =  " + const);
	}
}
Please Enter String =  Hello World

Number of Vowel Characters     =  3
Number of Consonant Characters =  7

使用函数统计字符串中的元音字母和辅音字母

计算字符串元音和辅音字母的代码与上面的代码相同。在这里,我们使用 Java 函数将元音和辅音的逻辑分开。

import java.util.Scanner;

public class CountVowCons4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String vowConsStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String =  ");
		vowConsStr = sc.nextLine();
		
		VowOrCons(vowConsStr.toLowerCase());
	}
	public static void VowOrCons (String vowConsStr) {
		int i, vwl, cnts;
		vwl = cnts = 0;
		char ch;
		
		for(i = 0; i < vowConsStr.length(); i++)
		{
			ch = vowConsStr.charAt(i);
			if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
				vwl++;
			}
			else if(ch >= 'a' && ch <= 'z') {
				cnts++;
			}
		}		
		System.out.println("\nNumber of Vowel Characters     =  " + vwl);
		System.out.println("Number of Consonant Characters =  " + cnts);

	}
}
Count Vowels and Consonants in a String using functions