참치코더의 꿈 메모장

2021/07/09 JAVA - 과제 - 숫자 야구 게임 본문

JAVA

2021/07/09 JAVA - 과제 - 숫자 야구 게임

참치깡 2021. 7. 9. 20:36
728x90
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
101
102
103
104
105
106
107
108
109
110
package c_statement;
 
import java.util.Scanner;
 
public class Baseball {
 
    public static void main(String[] args) {
        /*
         * 123
         * 135 : 1S 1B 1O
         */
        
        int usr_num1 = 0;
        int usr_num2 = 0;
        int usr_num3 = 0;
        
        int com_num1 = 0;
        int com_num2 = 0;
        int com_num3 = 0;
        
        int strick = 0;
        int ball = 0;
        int out = 0;
        
        Scanner scan = new Scanner(System.in);
        System.out.println("============================================");
        System.out.println("숫자 야구 게임입니다.>");
        
        //usr_num1 = Integer.parseInt(scan.nextLine()); //첫번째 숫자
        //usr_num2 = Integer.parseInt(scan.nextLine()); //두번째 숫자
        //usr_num3 = Integer.parseInt(scan.nextLine()); //세번째 숫자
        
        
        com_num1 = ((int)(Math.random()*10+ 1); //123 //첫번째 숫자
        com_num2 = ((int)(Math.random()*10+ 1); //123 //두번째 숫자
        com_num3 = ((int)(Math.random()*10+ 1); //123 //세번째 숫자
        
        while (true) {
            System.out.println("1부터 9까지 중복되지 않는 10이하 숫자 3개를 입력해주세요.>");
            usr_num1 = Integer.parseInt(scan.nextLine()); //첫번째 숫자
            usr_num2 = Integer.parseInt(scan.nextLine()); //두번째 숫자
            usr_num3 = Integer.parseInt(scan.nextLine()); //세번째 숫자
            if (usr_num1 == com_num1) {
                ++strick;
                System.out.println(strick+"스트라이크");
                
            }else if(usr_num1 == com_num2){
                ++ball;
                System.out.println(ball+"볼");
                
            }else if(usr_num1 == com_num3){
                ++ball;
                System.out.println(ball+"볼");
                
            }else{
                ++out;
                System.out.println(out+"아웃");
            }
            if (usr_num2 == com_num1){
                ++ball;
                System.out.println(ball+"볼");
                
            }else if(usr_num2 == com_num2){
                ++strick;
                System.out.println(strick+"스트라이크");
                
            }else if(usr_num2 == com_num3){
                ++ball;
                System.out.println(ball+"볼");
                
            }else{
                ++out;
                System.out.println(out+"아웃");
                
            }
            if (usr_num3 == com_num1){
                ++ball;
                System.out.println(ball+"볼");
                
            }else if(usr_num3 == com_num2){
                ++ball;
                System.out.println(ball+"볼");
                
            }else if(usr_num3 == com_num3){
                ++strick;
                System.out.println(strick+"스트라이크");
                
            }else{
                ++out;
                System.out.println(out+"아웃");
            }
            if(strick==3){
                System.out.println();
                System.out.println(strick+"스트라이크"+"승리!");
                System.out.println("게임 끝!");
                break;
                    //}else if(out>=3){
                        //System.out.println();
                        //break;
                }
            System.out.println(strick+"스트라이크!"+ball+"볼!"+out+"아웃!");
            strick = 0;
            ball = 0;
            out = 0;
        }
        System.out.println("=============================================");
    }
 
}
 
cs

* 제어문을 사용해서 프로그램 제작

728x90
Comments