본문 바로가기
IT/Java

Java (If else문 예제4_369게임_1~999까지입력)

by hjshims 2021. 4. 8.

369게임

1~999사이의 정수 값을 입력하여 3,6,9중

하나가 있는 경우 박수 짝!,

두개가 있는 경우에 박수 짝짝!,

세개가 있는 경우 박수 짝짝짝!을 출력

 

package P0408;

import java.util.Scanner;
//3,6,9중 하나가 있는 경우 박수짝, 두개면 박수짝짝, 세개면 박수짝짝짝
public class IfElse3692 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int number;
		int first=0, second=0, third=0;
		
		System.out.print("1~999사이의 숫자를 입력하세요>> ");
		number = scan.nextInt();
		
		if(number<0 || number>999) {
			System.out.print("숫자 범위를 넘었어요. 다시 입력해주세요>> ");
			number = scan.nextInt();
		}
		
		first = number/100;			//100의 자리 숫자 구하는 식
		second = (number%100)/10;	//10의 자리 숫자 구하는 식
		third = (number%100)%10;	//1의 자리 숫자 구하는 식
		
		//3,6,9를 제외한 10보다 작은 수를 입력 했을 때 else문으로 감 
		if(first==0) {
			first=1;
		}
		if(second==0) {
			second=1;
		}
		if(third==0) {
			third=1;
		}
		
		if(first%3==0 && second%3==0 && third%3==0) {
			System.out.println("박수 짝짝짝!");
		}
		else if(first%3==0 && second%3==0) {
			System.out.println("박수 짝짝!");
		}
		else if(first%3==0 && third%3==0) {
			System.out.println("박수 짝짝!");
		}
		else if(second%3==0 && third%3==0) {
			System.out.println("박수 짝짝!");
		}
		else if(first%3==0 || second%3==0 || third%3==0) {
			System.out.println("박수 짝!");
		}
		else {
			System.out.println("박수x");
		}
	}

}