문장은 어린왕자 했던 대로
sum a 그렇게 해도 되는데 변수가 많아
배열을 이용해도 아이디어 한번 내보기.
a~z까지 구해보기(숙제)
package myproject;
//실습: 어린왕자 텍스트 중 'a'의 개수 세기 대소문자 구분 안함
//1. charAt()으로 각 문자를 가져온다.
//2. 문자열 길이만큼 반복하면서 'a', 'A'를 만나면 sum_a를 1씩 증가한다.
//3. sum_a 출력한다.
public class StringPrince {
public static void main(String[] args) {
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;
int sum_space = 0;
// System.out.println(little);
// char ch = str.charAt();
// int sum_a = 0;
// char a = charAt(little);
for (int i = 0; i < little.length(); i++) {
if(little.charAt(i) == 'a' || little.charAt(i) == 'A')
sum_a++;
if(little.charAt(i) == ' ')
sum_space++;
}
System.out.println(sum_a);
System.out.println("\n" + sum_space + 1);
}
}
'백엔드 > JAVA' 카테고리의 다른 글
cmd 사용 근데 왜 한글 깨지지? ㅠㅠ (0) | 2023.10.12 |
---|---|
예제 5-7 145p (0) | 2023.10.12 |
int len , String sub 문자열에 있는 메서드 활용법 (0) | 2023.10.12 |
예제 5-4 (0) | 2023.10.12 |
최댓값 최솟값 구하기 for문과 if문 활용 (0) | 2023.10.12 |