ITWILL학원 : 9강 JAVA BY 윤미영강사 JAVA 369게임만들기 369게임을 만들어보았다. 일의 자리와 소수점첫번째자리를 나누어서 처리했다.
변수 input는 사용자가 scanner를 통해 입력하는 숫자이다. 변수 remainder는 숫자를 10으로 나눈뒤 소수점첫째짜리를 3,6,9인지확인한다.
함수 divideTen()는 10으로 나눠서 369인지 체크하는 반복되는 코드이므로 밖으로 뺐다. 함수를 어디로 빼야하는지몰랐는데 main함수밖이면서 class안으로 빼야하는 것이었다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public  class  game369   {	public  static  void  main (String[] args)   { 		Scanner sc = new  Scanner(System.in); 		System.out.println("1~99까지 정수를 입력해주세요" ); 		int  input = sc.nextInt(); 		int  remainder = input%10 ;   		if (remainder == 3 ){ 			divideTen(input); 		}else  if (remainder == 6 ){ 			divideTen(input); 		}else  if (remainder == 9 ){ 			divideTen(input); 		} 	} 	private  static  void  divideTen (int  input)  { 		if (input/10  == 3  || input/10  == 6  || input/10  == 9 ){  			System.out.println("박수짝짝" ); 		}else { 			System.out.println("박수짝" ); 		} 	} } 
 
강사님 코드는 아래와 같다. 박수치는 횟수를 카운드해서 if조건문으로 박수2번이면 박수짝짝을 입력하는 함수이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public  static  void  main (String[] args)   {	Scanner sc = new  Scanner(System.in); 	System.out.println("1~99까지 정수를 입력해주세요" ); 	int  num = sc.nextInt(); 	int  digit10 = num/10 ;  	int  digit1 = num%10 ;  	int  clapCnt = 0 ; 	if (digit1%3  == 0  && digit1 !=0 ){ clapCnt++; }  	if (digit10%3  == 0  && digit10 !=0 ){ clapCnt++; }  	if (clapCnt == 2 ){ 		System.out.println("박수짝짝" ); 	}else  if (clapCnt == 1 ){ 		System.out.println("박수짝" ); 	} }