Java/제어문

제어문(2)

은찡안찡 2023. 4. 5. 11:04

단순 if문 두번을 사용해서 숫자가 홀수인지 짝수인지 구분하여 출력하세요.

 

package com.dream.controls;


public class ControlEx02 {
	public static void main(String[] args) {
		int num = 9;
		String str = "";
		/*
		if(num % 2 == 0) 	str = "짝수";
		if(num % 2 == 1) str = "홀수";
		*/
		//양자 택일의 상황에서 사용
		if(num % 2 == 0) 	str = "짝수";
		else str = "홀수";
		
		System.out.println(num+"은 "+str+"이다.");
	}
}

'Java > 제어문' 카테고리의 다른 글

제어문(6)  (0) 2023.04.05
제어문(5)  (0) 2023.04.05
제어문(4)  (0) 2023.04.05
제어문(3)  (0) 2023.04.05
제어문(1)  (0) 2023.04.05