Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

bear's_story

JAVA 이중 if문 본문

JAVA

JAVA 이중 if문

S_bear 2021. 9. 8. 04:43
미션

입력받은 숫자가 양수인지 음수인지 0인지, 짝수인지 홀수인지 판단하기.

 

코드 및 해설
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        
        System.out.print("입력 숫자: ");
		int number = input.nextInt();
		
		if(number > 0) {
			System.out.print("양수이면서 ");
			if(number % 2 == 0)
				System.out.print("짝수입니다.");
			else
				System.out.print("홀수입니다.");
		}
		else if(number < 0) {
			System.out.print("음수이면서 ");
			if(number % 2 == 0)
				System.out.print("짝수입니다.");
			else
				System.out.print("홀수입니다.");
		}
		else
			System.out.print("0입니다.");
    }

}

if문 안에 조건이 성립할 시 출력문을 실행하고 다음 if문 조건을 확인한다. 조건이 성립하면 안에 내용을 실행하고 거짓이면 else문을 실행한다.

(if문 안에 한 줄만 있을 땐 중괄호 생략이 가능하다.)

 

출력 결과

'JAVA' 카테고리의 다른 글

JAVA switch문  (0) 2021.09.08
JAVA else if문  (0) 2021.09.08
JAVA if문  (0) 2021.09.08
JAVA 입력받고 출력하기(4)  (0) 2021.09.05
JAVA 입력받고 출력하기(3)  (0) 2021.09.05