Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để đếm số nguyên âm và phụ âm trong một câu

Trong bài này, chúng ta sẽ hiểu cách đếm các nguyên âm và phụ âm trong Java. Các bảng chữ cái bao gồm 'a' 'e' 'i' 'o' 'u' được gọi là Nguyên âm và tất cả các bảng chữ cái khác được gọi là Phụ âm.

Dưới đây là một minh chứng về điều tương tự -

Đầu vào

Giả sử đầu vào của chúng tôi là -

Hello, my name is Charlie

Đầu ra

Đầu ra mong muốn sẽ là -

The number of vowels in the statement is: 8
The number of vowels in the Consonants is: 12

Thuật toán

Step1- Start
Step 2- Declare two integers: vowels_count, consonants_count and a string my_str
Step 3- Prompt the user to enter a string value/ define the string
Step 4- Read the values
Step 5- Run a for-loop, check each letter whether it is a consonant or an vowel. Increment
the respective integer. Store the value.
Step 6- Display the result
Step 7- Stop

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để đếm số nguyên âm và phụ âm trong một câu .

import java.util.Scanner;
public class VowelAndConsonents {
   public static void main(String[] args) {
      int vowels_count, consonants_count;
      String my_str;
      vowels_count = 0;
      consonants_count = 0;
      Scanner scanner = new Scanner(System.in);
      System.out.println("A scanner object has been defined ");
      System.out.print("Enter a statement: ");
      my_str = scanner.nextLine();
      my_str = my_str.toLowerCase();
      for (int i = 0; i < my_str.length(); ++i) {
         char ch = my_str.charAt(i);
         if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            ++vowels_count;
         }
         else 
         if ((ch >= 'a' && ch <= 'z')) {
            ++consonants_count;
         }
      }
      System.out.println("The number of vowels in the statement is: " + vowels_count);
      System.out.println("The number of vowels in the Consonants is: " + consonants_count);
   }
}

Đầu ra

A scanner object has been defined
Enter a statement: Hello, my name is Charlie
The number of vowels in the statement is: 8
The number of vowels in the Consonants is: 12

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class VowelAndConsonents {
   public static void main(String[] args) {
      int vowels_count, consonants_count;
      vowels_count = 0;
      consonants_count = 0;
      String my_str = "Hello, my name is Charie";
      System.out.println("The statement is defined as : " +my_str );
      my_str = my_str.toLowerCase();
      for (int i = 0; i < my_str.length(); ++i) {
         char ch = my_str.charAt(i);
         if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            ++vowels_count;
         } 
         else 
         if ((ch >= 'a' && ch <= 'z')) {
            ++consonants_count;
         }
      }
      System.out.println("The number of vowels in the statement is: " + vowels_count);
      System.out.println("The number of vowels in the Consonants is: " + consonants_count);
   }
}

Đầu ra

The statement is defined as : Hello, my name is Charie
The number of vowels in the statement is: 8
The number of vowels in the Consonants is: 11