Programming Language/JAVA

[Java] Checking whether the Character is vowel or consonant. / 문자가 모음인지 자음인지 판별하기

나수비니 2023. 10. 10. 21:54
728x90

For any given character, I need to check if it is a vowel or consonant.

Example

Input : char = 'r'
Output : Constant

Input : char = 'e'
Output : Vowel

I will check if the stated character corresponds to any of the five vowels. And if it matches, “Vowel” is printed, else “Consonant” is printed.

 

Step.1 문제 정의 (Define the problem)

  • 알파벳이 자음이라면 ‘Consonant’ 출력 모음이라면 ‘Vowel’ 출력

Step.2 브레인스토밍 (Brainstorm)

  1. How can I make the entire alphabet?
  2. How can I distinguish between “vowel” and “constant”?
  3. How can I compare “vowel” and “consonant” simultaneously?
  4. How can I determine if a letter is a vowel or a consonant?
  5. Do I need to create an alphabet list first?

Step.3 결정 (Make a decision)

  1. 변수 타입을 ‘Char’로 선언한다.
  2. The uppercase condition is set using an IF statement.

Step.4 실행 (Implement)

package toCheckwhetherThecharacterIs;

public class geek {

	
	//fucntion to find whether an input character is vowel_or_not
	static void Vowel_Or_Constant(char cons) {
	
	if(cons == 'A' || cons == 'E' || cons == 'I' || cons == 'O' || cons == 'U')		
	System.out.print("It's a vowel");
	else {
	System.out.println("It's a consonant.");
		}
	} 

	//the driver code
	public static void main(String[] args) {

		Vowel_Or_Constant('H');		
	}
}

Output

It's a consonant.

Step.6 평가 (Evaluate)

  • 다양한 방법들이 더 있다.
    1. Switch 문으로 풀어보기
    2. Case 문으로 풀어보기