C#/수업내용

2020.04.06. 수업내용 - for문, if문 5 (가위바위보)

dev_sr 2020. 4. 7. 00:08

 

8. 

1) 가위, 바위, 보 중에 선택하기

2) 컴퓨터는 랜덤으로 가위, 바위, 보 중에 하나를 냄.

3) 비교해서 무승패 가르기

4) 무승패 통계내기

 

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
             //8. 가위바위보 게임 (승, 무, 패 합산)
 
 
            int win = 0;
            int lose = 0;
            int draw = 0;
 
 
            for (; ;)
            {
                Console.Write("1: 가위, 2: 바위, 3: 보  ");
 
                var gameNum=Console.ReadLine();
 
                if(gameNum=="1")
                {
                    Console.WriteLine("당신은 가위를 냈습니다.\n");
                }
                else if (gameNum == "2")
                {
                    Console.WriteLine("당신은 바위를 냈습니다.\n");
                }
                else if (gameNum == "3")
                {
                    Console.WriteLine("당신은 보를 냈습니다.\n");
                }
 
                Random rand = new Random();
                int comNum = rand.Next(14);
 
                if(comNum == 1)
                {
                    Console.WriteLine("컴퓨터가 가위를 냈습니다.\n");
 
                    if(gameNum=="1")
                    {
                        Console.WriteLine("비겼습니다.\n");
                        draw++;
                    }
 
                    else if(gameNum=="2")
                    {
                        Console.WriteLine("이겼습니다.\n");
                        win++;
                    }
 
                    else if (gameNum == "3")
                    {
                        Console.WriteLine("졌습니다.\n");
                        lose++;
                    }
                }
                else if (comNum == 2)
                {
                    Console.WriteLine("컴퓨터가 바위를 냈습니다.\n");
 
                    if (gameNum == "1")
                    {
                        Console.WriteLine("졌습니다.\n");
                        lose++;
                    }
 
                    else if (gameNum == "2")
                    {
                        Console.WriteLine("비겼습니다.\n");
                        draw++;
                    }
 
                    else if (gameNum == "3")
                    {
                        Console.WriteLine("이겼습니다.\n");
                        win++;
                    }
                }
                else if (comNum == 3)
                {
                    Console.WriteLine("컴퓨터가 보를 냈습니다.\n");
 
                    if (gameNum == "1")
                    {
                        Console.WriteLine("이겼습니다.\n");
                        win++;
                    }
 
                    else if (gameNum == "2")
                    {
                        Console.WriteLine("졌습니다.\n");
                        lose++;
                    }
 
                    else if (gameNum == "3")
                    {
                        Console.WriteLine("비겼습니다.\n");
                        draw++;
                    }
                }
 
                Console.WriteLine("승: {0}, 무: {1}, 패: {2}", win, draw, lose);
                Console.WriteLine();
 
            }
 

 

 

8. 결과 값