출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        
        Scanner sc = new Scanner(System.in);
        int a ; // 랜덤으로 정해질 숫자
        int b ; // 유저로부터 입력받을 숫자
        a=(int)(Math.random()*100)+1;
        
        for(int i=0 ; i<10 ; i++){ // 10번의 기회만 줄것
            System.out.print("숫자를 입력하세요 (1~100): ");
            b=sc.nextInt();
                if(b>a) {
                    System.out.println("정답이 입력한 숫자보다 작습니다");}
                else if(b<a) {
                    System.out.println("정답이 입력한 숫자보다 큽니다");}
                else {
                    System.out.println("정답입니다");
                    break;}
                if(i==9) {
                    System.out.println("10번의 기회를 모두 사용하셨습니다.");
                }
            
    
cs

 

- 여러가지 요소 설정

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Scanner sc = new Scanner(System.in);
        
        /*
            숫자 찾기 게임
            
            random number
            범위: 1 ~ 100 -> number(71)            
                        
            user 입력: 50
            -> 너무 작습니다
            user 입력: 75
            -> 너무 큽니다
            user 입력: 65
            -> 너무 작습니다
            user 입력: 70
            -> 너무 작습니다
 
        */
        
        // 1.변수의 선언
        int r_num, u_num;
        boolean clear;
        String s_num;
        
        clear = false;
        
        // 2.랜덤 수의 산출(1 ~ 100)
        r_num = (int)(Math.random() * 100+ 1;
        System.out.println("r_num:" + r_num);
        
        // ------------------------ loop -> 10회
        boolean b = false;
        int count = 0;
        
        while(count < 10) {
        
            // 3.유저의 입력
            while(true) {    
                b = false;
                System.out.print((count + 1+ "회 숫자입력 = ");
            //    u_num = sc.nextInt();
                s_num = sc.next();        
                // System.out.println( s_num.length() );
                // abc
                for(int i = 0;i < s_num.length(); i++) {
                    char c = s_num.charAt(i);
                //    System.out.println("c = "+ c);
                    
                    /*
                    if(c != '0' && c != '1' && c != '2' &&
                        c != '3' && c != '4' && c != '5' &&
                        c != '6' && c != '7' && c != '8' &&
                        c != '9') {
                        System.out.println("정상적인 숫자를 입력해 주십시오");
                        b = true;
                        break;
                    }
                    */
                    
                    int asc = (int)c;
                    if(asc < 48 || asc > 57) { // 숫자의 범위
                        System.out.println("정상적인 숫자를 입력해 주십시오");
                        b = true;
                        break;
                    }                    
                }            
                if(b == false) {    // 문제 없음
                    //System.out.println("문제없이 입력함");
                    break;
                }    
                
                System.out.println("b = " + b);
            }
            
            // 문자열 -> 숫자
            u_num = Integer.parseInt( s_num );
                    
            // 4.판정(크다/작다)
            if(u_num > r_num) {
                System.out.println("너무 큽니다");
            }
            else if(u_num < r_num) {
                System.out.println("너무 작습니다");
            }
            else {    // 숫자를 맞춘경우
                clear = true;
                break;
            }
            
            count++;
        }
        // ------------------------
        
        // 5.결과
        if(clear) {
            System.out.println("Game Clear!");            
        }
        else {
            System.out.println("Game Over~");
        }
cs

+ Recent posts