从方法返回枚举值

本文关键字:枚举 返回 方法 | 更新日期: 2023-09-27 17:59:10

嗨,我正在尝试制作一个简单的霰弹枪游戏,其中用户与CPU以及两者都选择射击、防护或重新加载,但在我的GetOptionFromUser方法中,我不确定如何根据用户的选择从枚举方法返回值。

如有任何指导,我们将不胜感激这是我的方法

enum ShotgunOption
         {
             Shoot = 1,
             Reload = 2,
             Shield = 3

         }
        static void DisplayMenu()
        {
            Console.WriteLine("Please pick an item:");
            Console.WriteLine("S - Shoot");
            Console.WriteLine("P - Shield");
            Console.WriteLine("R - Reload");         
            Console.WriteLine("X - Exit");
        }
        static ShotgunOption GetOptionFromUser()
        {
            char menuItem;
            DisplayMenu();
            Console.WriteLine("Please Select A Option");
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            while (menuItem != 'S' && menuItem != 'P' &&
                menuItem != 'R' && menuItem != 'X')
            {
                Console.WriteLine("Error - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
            return ShotgunOption;
        }
        static void DisplayResults(ShotgunOption UserOption,ShotgunOption CPUOption, int UserScore, int UserBullets, int CPUBullets)
    {
        Console.Clear();
        Console.WriteLine("Giving up?");
        Console.WriteLine("You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s).", UserOption, CPUOption, UserScore, UserBullets, CPUBullets);
        Console.WriteLine("Thanks for playing!");
        Console.ReadKey();
    }

从方法返回枚举值

在方法GetOptionFromUser中,您需要检查用户选择了什么。在此基础上,您返回ShotgunOption.Shot、ShotgunOption、Reload或ShotgunOption.Shield。类似于以下内容:

if (menuItem == 'S')
     return ShotgunOption.Shoot;
if (menuItem == 'P')
     return ShotgunOption.Shield;

除此之外,还要考虑这一点:

1) 将用户界面(UI)与应用程序的逻辑分离。在UI层中,您应该只检查用户输入,验证好的请求并将其传递给业务层。业务层将完成工作并将结果返回到UI层。

2) 不要一直使用"静态"。只有当您不需要对象,或者希望在同一时间的对象之间共享值,或者类只有不依赖于对象状态的方法时,静态才有意义。

您需要使用switch语句(或一系列if语句)来返回取决于所选菜单值的显式枚举值。像这样:

    static ShotgunOption GetOptionFromUser()
    {
        char menuItem;
        DisplayMenu();
        Console.WriteLine("Please Select A Option");
        menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
        while(true)
        {
            switch(menuItem)
            {
                case 'S': return ShotgunOption.Shoot;
                case 'P': return ShotgunOption.Shield;
                case 'R': return ShotgunOption.Reload;
                case 'X': return ShotgunOption.Exit;
                default:
                    Console.WriteLine("Error - Invalid menu item");
                    DisplayMenu();
                    menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
        }
        return ShotgunOption.Exit; // to keep compiler happy
    }

您还可以利用这样一个事实,即字符本身可以被视为数字,并且这些数字可以直接分配给枚举:

  enum ShotgunOption
        {
            Shoot = 'S',
            Reload = 'R',
            Shield = 'P'
        }

这样,可以通过查看是否定义了值来进行检查。以您的char菜单项为例:

 // char menuitem...
 //X should be checked before and handled appropiately, or else added in the enumeration
 if (Enum.IsDefined(typeof(ShotgunOption), (int)menuItem ))
            return (ShotgunOption)menuItem ;
 //throw exception, return nullable<shotgunoption> or print error message. 

如上所述,只是一些思考:如果你定义了一个包含菜单键、描述和操作的类,而不是枚举,你可以使用该类的列表来显示菜单,确定所选的操作,并通过在类本身中实现来执行该操作。