蓝#C#课后习题:定义一个枚举类型PlayerStates用来表示玩家的动作,包括跑,跳,下滑,左转,右转,结合Switch语句使用,写一个小程序:按下W跑,空格跳,S下滑,A左转,D右转
01 xinlusing System;
02
03 namespace C_enum_lianxi
04 {
05 public enum PlayerStates{
06 Pao,
07 Tiao,
08 XiaHua,
09 ZuoZhuan,
10 YouZhuan,
11 }
12 class MainClass
13 {
14 public static void Main (string[] args)
15 {
16
17 PlayerStates State = PlayerStates.Pao;
18 string Str = Console.ReadKey ().KeyChar.ToString ();
19 if(Str=="W"||Str=="w")
20 {
21 State = PlayerStates.Pao;
22
23 }else if(Str==" ")
24 {
25 State = PlayerStates.Tiao;
26
27 }else if(Str=="S"||Str=="s")
28 {
29 State = PlayerStates.XiaHua;
30
31 }else if(Str=="A"||Str=="a")
32 {
33 State = PlayerStates.ZuoZhuan;
34
35 }
36 else if(Str=="D"||Str=="d")
37 {
38 State = PlayerStates.YouZhuan;
39 }
40
41
42 switch(State)
43 {
44 case PlayerStates.XiaHua:
45 Console.WriteLine ("XiaHua");
46 break;
47 case PlayerStates.Tiao:
48 Console.WriteLine ("Tiao");
49 break;
50 case PlayerStates.ZuoZhuan:
51 Console.WriteLine ("ZuoZhuan");
52 break;
53 case PlayerStates.YouZhuan:
54 Console.WriteLine ("YouZhuan");
55 break;
56 case PlayerStates.Pao:
57 Console.WriteLine ("Pao");
58 break;
59
60 }
61 }
62 }
63 }
01 xinlusing System;
02
03 namespace C_enum_lianxi
04 {
05 public enum PlayerStates{
06 Pao,
07 Tiao,
08 XiaHua,
09 ZuoZhuan,
10 YouZhuan,
11 }
12 class MainClass
13 {
14 public static void Main (string[] args)
15 {
16
17 PlayerStates State = PlayerStates.Pao;
18 string Str = Console.ReadKey ().KeyChar.ToString ();
19 if(Str=="W"||Str=="w")
20 {
21 State = PlayerStates.Pao;
22
23 }else if(Str==" ")
24 {
25 State = PlayerStates.Tiao;
26
27 }else if(Str=="S"||Str=="s")
28 {
29 State = PlayerStates.XiaHua;
30
31 }else if(Str=="A"||Str=="a")
32 {
33 State = PlayerStates.ZuoZhuan;
34
35 }
36 else if(Str=="D"||Str=="d")
37 {
38 State = PlayerStates.YouZhuan;
39 }
40
41
42 switch(State)
43 {
44 case PlayerStates.XiaHua:
45 Console.WriteLine ("XiaHua");
46 break;
47 case PlayerStates.Tiao:
48 Console.WriteLine ("Tiao");
49 break;
50 case PlayerStates.ZuoZhuan:
51 Console.WriteLine ("ZuoZhuan");
52 break;
53 case PlayerStates.YouZhuan:
54 Console.WriteLine ("YouZhuan");
55 break;
56 case PlayerStates.Pao:
57 Console.WriteLine ("Pao");
58 break;
59
60 }
61 }
62 }
63 }