개발일기/TIL(Since24.04.19)

프로그래머스 홀짝 구분하기

w.llama 2024. 8. 27. 09:26

문제 링크 
https://school.programmers.co.kr/learn/courses/30/lessons/181944

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 풀이 1

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        if(n % 2 == 1){
            System.out.print(n + " is odd");
        }else if(n % 2 ==0){
            System.out.print(n + " is even");
        }else{
            System.out.print("error");
        }
    }
}

 

문제 풀이 2

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        System.out.print(n + " is "+(n % 2 == 0 ? "even" : "odd"));

    }
}

 

 

삼항연산자를 자주 쓰지 않다보니 생각하지 못했던 것 같다