티스토리 뷰

 

 

쉬우니까 코드만 첨부함니당

 

 

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==0){
            System.out.println(n+" is even");
        }else{
            System.out.println(n +" is odd" );
        }
    }    
}

 

초간단 if문 사용

 

 

 

 

 

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"));
    }
}

 

다른 분들 풀이 봤더니 삼항연산자 사용하면 한줄로도 가능합니다

배운거 써먹어야되는데,,,,,,,,

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함