C#/과제

2020.04.08. 종족, 직업 선택하기 (switch, 열거형 변환)

dev_sr 2020. 4. 8. 18:35
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
111
112
113
114
115
116
        enum Race
        {
            Human,
            Dwarf,
            NightElf
        }
 
        enum ClassTypes
        {
            Warrior,
            Rogue,
            Hunter,
            Priest,
            Mage,
            Monk
        }
      
 
        public App()
        {
 
            Console.Write("(0: Human, 1:Dwarf, 2: NightElf)\n종족을 선택해주세요.  ");
            var raceInput=Int32.Parse(Console.ReadLine());
            var race = (Race)raceInput;
 
            switch(race)
            {
                case Race.Human:
                    {   //{  } 블록으로 묶으면 같은 이름의 변수를 다른 case에서 또 사용할수 있다
                        Console.WriteLine("{0}을 선택 했습니다."Race.Human);
 
                        Console.Write("(0: Warrior, 1: Rogue) \n직업을 선택 해주세요: ");
                        var classTypeInput = Int32.Parse(Console.ReadLine());
                        var classType = (ClassTypes)classTypeInput;
 
                        switch (classType)
                        {
                            case ClassTypes.Warrior:
                                Console.WriteLine("캐릭터가 ({0},{1})가 생성되었습니다."Race.Human, ClassTypes.Warrior);
                                break;
 
                            case ClassTypes.Rogue:
                                Console.WriteLine("캐릭터가 ({0},{1})가 생성되었습니다."Race.Human, ClassTypes.Rogue);
                                break;
                        }
 
                    }
 
                    break;
 
                case Race.Dwarf:
                    {
                        Console.WriteLine("{0}을 선택 했습니다."Race.Dwarf);
 
                        Console.Write("(0: Hunter, 1: Priest) \n직업을 선택 해주세요: ");
                        var classTypeInput = Int32.Parse(Console.ReadLine());
                        if(classTypeInput==0)
                        {
                            classTypeInput =2;
                        }
 
                        else if (classTypeInput == 1)
                        {
                            classTypeInput = 3;
                        }
                        var classType = (ClassTypes)classTypeInput;
                       
 
                        switch (classType)
                        {
                            case ClassTypes.Hunter:
                                Console.WriteLine("캐릭터가 ({0},{1})가 생성되었습니다."Race.Dwarf, ClassTypes.Hunter);
                                break;
 
                            case ClassTypes.Priest:
                                Console.WriteLine("캐릭터가 ({0},{1})가 생성되었습니다."Race.Dwarf, ClassTypes.Priest);
                                break;
                        }
                    }
                    break;
 
                case Race.NightElf:
                    {
                        Console.WriteLine("{0}을 선택 했습니다.", Race.NightElf);
 
                        Console.Write("(0: Mage, 1: Monk) \n직업을 선택 해주세요: ");
                        var classTypeInput = Int32.Parse(Console.ReadLine());
                        if (classTypeInput == 0)
                        {
                            classTypeInput = 4;
                        }
 
                        else if (classTypeInput == 1)
                        {
                            classTypeInput = 5;
                        }
                        var classType = (ClassTypes)classTypeInput;
 
                        switch (classType)
                        {
                            case ClassTypes.Mage:
                                Console.WriteLine("캐릭터가 ({0},{1})가 생성되었습니다.", Race.NightElf, ClassTypes.Mage);
                                break;
 
                            case ClassTypes.Monk:
                                Console.WriteLine("캐릭터가 ({0},{1})가 생성되었습니다.", Race.NightElf, ClassTypes.Monk);
                                break;
                        }
                    }
                    break;
 
                default:
                    break;
 
            }
 
 

 

 

결과값