Java 程序:计算字符串中元音和辅音总数

原文:https://www.studytonight.com/java-programs/java-program-to-count-the-total-number-of-vowels-and-consonants-in-a-string

在本教程中,我们将学习如何计算一个字符串中元音的总数和计数。但是在进一步深入之前,如果你不熟悉字符串的概念,那么一定要查看 Java 中Strings的文章。

输入:输入字符串:今晚学习

输出:元音总数为 3

程序 1:计算一个字符串中元音和辅音的总数

在这个程序中,我们将看到当字符串在程序中被预定义时,如何计算一个字符串中元音和计数的总数。

算法

  1. 开始
  2. 声明字符串
  3. 初始化它。
  4. 将字符串转换为小写。
  5. 声明两个变量(用于元音计数的 vcount 和用于辅音计数的 ccount)来计算字符串中的元音和辅音,并将其初始化为 0。
  6. 使用 for 循环遍历字符串的每个字符。
  7. 使用 if 条件检查是否有任何字符与字母表中的元音匹配。
  8. 如果遇到任何元音,则增加 vcount。
  9. 否则,如果遇到任何辅音,则增加计数。
  10. 显示两个计数变量的值。
  11. 停下来。

下面是相同的 Java 语言代码。

//Java Program to find the total number of vowels and consonants with pre-defined values
public class Main
{
     public static void main(String []args)
     {
        String str="Study Tonight";    //Given String
        System.out.println("The String is: "+str);

        int vcount=0,ccount=0;    //Variables to count the vowels and consonants
        str=str.toLowerCase();    //Convert the string to lowercase        
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u')
            vcount++;  //Increment each time vowel encounters
            else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
            ccount++;   //Increment each time consonant encounters
        }        
        //Print the total number of vowels
        System.out.println("The total number of vowels is: "+vcount);
        //Print the total number of consonants
        System.out.println("The total number of consonants is: "+ccount);        
     }
}

字符串为:今晚学习 元音总数为:3 辅音总数为:9

程序 2:计算一个字符串中元音和辅音的总数

在这个程序中,我们将看到当字符串是用户定义的时,如何计算字符串中元音和计数的总数。这意味着,这里我们将要求用户初始化字符串,然后我们将统计输入字符串的元音和辅音的总数。

算法

  1. 开始
  2. 声明字符串
  3. 要求用户初始化字符串。
  4. 将字符串转换为小写。
  5. 声明两个变量(用于元音计数的 vcount 和用于辅音计数的 ccount)来计算字符串中的元音和辅音,并将其初始化为 0。
  6. 使用 for 循环遍历字符串的每个字符。
  7. 使用 if 条件检查是否有任何字符与字母表中的元音匹配。
  8. 如果遇到任何元音,则增加 vcount。
  9. 否则,如果遇到任何辅音,则增加计数。
  10. 显示两个计数变量的值。
  11. 停下来。

下面是相同的 Java 语言代码。

//Java Program to find the total number of vowels and consonants with user-defined values
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the String: ");
        String str=sc.nextLine();    //Initialize the String        
        int vcount=0,ccount=0;    //Variables to count the vowels and consonants
        str=str.toLowerCase();    //Convert the string to lowercase        
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u')
            vcount++;  //Increment each time vowel encounters
            else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
            ccount++;   //Increment each time consonant encounters
        }        
        //Print the total number of vowels
        System.out.println("The total number of vowels is: "+vcount);
        //Print the total number of consonants
        System.out.println("The total number of consonants is: "+ccount);        
     }
}

入弦:今晚学习 元音总数为:3 辅音总数为:9