문제 링크
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"));
}
}
삼항연산자를 자주 쓰지 않다보니 생각하지 못했던 것 같다
'개발일기 > TIL(Since24.04.19)' 카테고리의 다른 글
프로그래머스 더크게 합치기 (0) | 2024.09.01 |
---|---|
프로그래머스 문자열 섞기 (1) | 2024.08.28 |
프로그래머스 대소문자 변환 (0) | 2024.08.26 |
Java&Spring -TIL(24.07.12 금) (0) | 2024.07.12 |
Java&Spring -TIL(24.07.11 목) (0) | 2024.07.11 |