
내가 생각했을때 문제에서 원하는부분
The first line in the test data file contains the number of test cases (< 100).
After that, each line contains one test case, a word, w, (provided as a String).
You can assume that the words are provided in all lowercase letters (i.e., “gru”, not “Gru”).
For each test case, you are to output the number of vowels in that word.
The vowels are: “a”, “e”, “i”, “o”, and “u”.
The exact format is shown below in the examples.
내가 이 문제를 보고 생각해본 부분
입력을 빠르고 편리하게 처리하기 위해 BufferedReader를 사용한다.
첫 줄에서 테스트 케이스 수인 N을 정수로 읽는다.
for문을 이용해 각 테스트 케이스마다 단어를 한 줄 읽는다.
읽은 단어의 각 문자(charAt)를 순회하며 모음인지 확인하는 조건문을 실행한다.
해당 문자가 모음이면 count를 1 증가시킨다.
각 단어에 대해 모음 개수를 다 셌으면 포맷에 맞게 출력한다.
출력 시에는 printf를 활용하여 지정된 형식 "The number of vowels in [word] is [count]."로 출력한다.
마지막에 BufferedReader를 닫아 자원을 정리한다.
코드로 구현
package baekjoon.baekjoon_32;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 백준 10203번 문제
public class Main1295 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); // 테스트 케이스 수 입력
for (int i = 0; i < N; i++) {
String word = br.readLine(); // 단어 입력
int count = 0;
for (int j = 0; j < word.length(); j++) {
char ch = word.charAt(j);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.printf("The number of vowels in %s is %d.%n", word, count);
}
br.close();
}
}
코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.