본문 바로가기
c#/수업내용

2020.04.09 enum과 switch 사용 weapon

by Luna_O 2020. 4. 9.

실행코드

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_006
{
    class App
    {
        enum weaponType
        {
            Sword, Bow, Axe
        }
        public App()
        {
            //Console.WriteLine("2020.04.09");
 
            //소지중인 아이템 : Sword, Axe 출력하기
            Console.WriteLine("소지중인 아이템 : Sword, Axe");
            while (true)
            {
                //강화하시려는 무기의 이름을 입력해주세요. 출력 후 물어보기
                Console.Write("강화하시려는 무기의 이름을 입력해주세요. ");
                var inputWeapon = Console.ReadLine();
                var Weapon = Enum.Parse(typeof(weaponType), inputWeapon);
 
                switch (Weapon)
                {    //만약에 강화 할 무기로 Sword를 입력한다면
                    case weaponType.Sword:
                        //강화하시려면 "강화"를 입력해주세요. 출력 후 물어보기
                        Console.Write("강화하시려면 \"강화\"를 입력해주세요.");
                        string input = Console.ReadLine();
                        //(강화 확률은 Sword : 30%, Bow : 25%, Axe : 20% 입니다) 출력
                       
                        //만약에 강화를 입력한다면 확률은 30% 랜덤 설정해주기
                        switch (input)
                        {
                            case "강화":
                                Console.WriteLine("(강화 확률은 Sword : 30%, Bow : 25%, Axe : 20% 입니다)");
                                var rand = new Random();
                                var consolidationPer = rand.Next(1101);      // 1~ 100 
                                if (consolidationPer <= 30)
                                {
                                    Console.WriteLine("아이템 (Sword) 강화에 성공했습니다.");
                                    Console.WriteLine("소지중인 아이템 : {0}(+1),Axe", Weapon);
                                }
                                else
                                {
                                    Console.WriteLine("아이템 (Sword) 강화에 실패했습니다.");
                                    Console.WriteLine("소지중인 아이템 : {0},Axe", Weapon);
                                    continue;
                                }
                                break;
 
                            default:
                                Console.WriteLine("잘못된 명령어입니다.");
                                continue;
 
                        }
                        break;
                    case weaponType.Axe:
                        //강화하시려면 "강화"를 입력해주세요. 출력 후 물어보기
                        Console.Write("강화하시려면 \"강화\"를 입력해주세요.");
                        string input1 = Console.ReadLine();
                        //(강화 확률은 Sword : 30%, Bow : 25%, Axe : 20% 입니다) 출력
                        //만약에 강화를 입력한다면 확률은 30% 랜덤 설정해주기
                        switch (input1)
                        {
                            case "강화":
                                Console.WriteLine("(강화 확률은 Sword : 30%, Bow : 25%, Axe : 20% 입니다)");
                                var rand = new Random();
                                var consolidationPer = rand.Next(1101);      // 1~ 100 
                                if (consolidationPer <= 20)
                                {
                                    Console.WriteLine("아이템 {0} 강화에 성공했습니다.", Weapon);
                                    Console.WriteLine("소지중인 아이템 : Sword, {0}(+1)", Weapon);
                                }
                                else
                                {
                                    Console.WriteLine("아이템 {0} 강화에 실패했습니다.", Weapon);
                                    Console.WriteLine("소지중인 아이템 : Sword, {0}", Weapon);
                                    continue;
                                }
                                break;
 
                            default:
                                Console.WriteLine("잘못된 명령어입니다.");
                                continue;
                        }
                        break;
 
                    default:
                        Console.WriteLine("입력하신 무기는 소지하고 있지않습니다.");
                        continue;
                }
                break;
            }
 
 

실행창