Coding Class
231012_JAVA
Seungyeon.Jung
2023. 10. 12. 21:34
실습 내용: 어른왕자 텍스트 중 대소문자를 구분하지 않고 a의 갯수를 세어볼 것
package Assignment;
public class Training_1012_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//실습: 어린왕자 텍스트 중 'a'의 개수 세기 대소문자 구분 안함
//1. charAt()으로 각 문자를 가져온다.
//2. 문자열 길이만큼 반복하면서 'a', 'A'를 만나면 sum_a를 1씩 증가한다.
//3. sum_a 출력한다.
String little = "Once when I was six years old I saw a magnificent picture in a book,"
+ "called True Stories from Nature, about the primeval forest."
+ "nIt was a picture of a boa constrictor in the act of swallowing an animal.
Here is a copy of the drawing. ";
int sum_a = 0; //'A' or 'a' 나타난 횟수 저장
for (int i = 0; i < little.length(); i++) {
//i 를 초기화? 한 뒤 little.length() 을 통해 String little 의 문장을 읽어들임.
//i 를 0으로 초기화 하고 읽어들인 문장을 i에 부여한 뒤, 문장의 길이만큼 반복한다.
//반복하는 이유는 문자열의 문자에 하나씩 접근하기 위함임.
if(little.charAt(i) == 'a' || little.charAt(i) == 'A')
//i(읽어들인 문장) 에서 대문자 A or 소문자 a 일 경우
sum_a++; //sum_a 을 1씩 증가, 즉 'A' or 'a' 의 개수를 셈.
}
System.out.println("단어 a(A) 는..." + sum_a + " 개 "); //a개수 출력
}
}
복잡한 내용에 비해 너무나도 심플한 결과물.