package com.croute.hw_1_2;

 

import java.util.Scanner;

import java.util.Random;

 

public class HW_1_2 {

 

        /**

         * @param args

         */

       

        public static int count = 10;

        public static int min = 1;

        public static int max = 200;

       

        public static void main(String[] args) {

               // TODO Auto-generated method stub

               int input = 0;

               int checking_state = 0;

               int secretNumber = 0;

               int left = 1, right = 200; // 왼쪽범위와오른쪽범위

              

               // 난수발생 // seed값을배정하여생성   

               Random rand = new Random(System.currentTimeMillis());            

               secretNumber = Math.abs(rand.nextInt(max)+min);             

               // System.out.println("숫자: " + secretNumber);

               // 메뉴

               System.out.println("숨겨진숫자를맞추는프로그램입니다.");

               System.out.println("1. 숨겨진숫자를맞춰야합니다.");

               System.out.println("2. 열번의기회안에맞추지못하면프로그램이종료됩니다.");

               System.out.println("");

              

               while ( count > 0 ){  

                      

                       System.out.print("예상되는숫자를입력하세요(종료:999) : ");

                       Scanner scn = new Scanner(System.in);

                       input = scn.nextInt();

                       checking_state = checking_number(input, secretNumber);

                       count--;

                       if ( input == 999 ) {

                              System.out.print("프로그램을종료합니다.");

                              break;

                       }

                       else {

                              switch ( checking_state )

                              {

                                      case 1 :

                               if ( input < right ) right = input;                                        

                               System.out.println("입력한수보다더작은수입니다.");

                               System.out.println("숫자범위: " + left + " < x < " + right);

                                             System.out.println("남은기회: " + count);

                                             break;

                                      case 2 :

                                             if ( input > left ) left = input;

                                           System.out.println("입력한수보다더큰수입니다.");

                               System.out.println("숫자범위: " + left + " < x < " + right);

                                             System.out.println("남은기회: " + count);

                                             break;

                                      case 3 :                                    

                                             System.out.println("남은기회: " + count);

                System.out.println("축하합니다. 숨겨진숫자는" + secretNumber + " 입니다.");

                                             // 난수발생

                                             rand = new Random(System.currentTimeMillis());

                                            secretNumber = Math.abs(rand.nextInt(max)+min);

                                             System.out.println("");     

                                             break;

                                      default :

                                             break;

                              }

                              System.out.println("");

                       }

               }

        }

 

        public static int checking_number(int input, int secretNumber){

               int check = 0;        

              

               if ( input > secretNumber )           check = 1;

               else if ( input < secretNumber )      check = 2;

               else if ( input == secretNumber )     check = 3;            

              

               return check;         

        }

}

 

Posted by croute

댓글을 달아 주세요