[Java] 타입별 값 비교하기

Yeoonnii·2022년 12월 11일
0

Java

목록 보기
11/30
post-thumbnail

Java에서 기본타입 byte, short, int, long, float, double, boolean 의 값이 동일한지 비교할 때에는 ==을 사용하고, 문자열 String이 동일한지 비교할 때에는 equals()메소드를 사용한다.

백준 1264번 모음의 개수

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(true){
            String str = sc.nextLine();

            if(str.equals("#")){
                break;
            }

            char[] index = {'A','E','I','O','U','a','e','i','o','u'};
            int cnt = 0;

            for(int i=0; i<str.length(); i++){
                char c = str.charAt(i);
                for(int j=0; j<index.length; j++){
                    if(c == index[j]){
                        cnt++;
                    }
                }
            }
        System.out.println(cnt);
        }
        sc.close();
	}    
}

위 풀이에서 str이 #을 입력받는 시점에 while 반복을 중지하게 되는데
str은 String 타입의 변수이기 때문에 문자열이 동일한지 비교하는 equals()메소드를 사용해야 한다
str.equals("#")

0개의 댓글