本文介绍了返回方法中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试制作一个简单的猎枪游戏,其中用户与CPU以及两者都选择镜头,屏蔽或重新加载但是在我的GetOptionFromUser方法中,我不知道如何根据用户选择的方式从枚举方法返回值。 任何指导都会受到赞赏这是我的方法I am trying to make a simple shotgun game where the user vs the CPU and the both pick shot, shield or reload but In my GetOptionFromUser method I am not sure how to return value from the enumeration method depending on what the user chose.Any Guidance would be appreciated Here is my methodsenum 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(); menuItem = char.ToUpper(char.Parse(Console.ReadLine())); if (menuItem == 'S') { return ShotgunOption.Shoot; } else if (menuItem == 'P') { return ShotgunOption.Shield; } else if (menuItem == 'R') { return ShotgunOption.Reload; } while (menuItem != 'F' && menuItem != 'C' && menuItem != 'I' && menuItem != 'X' && menuItem != 'R' && menuItem != 'D') { Console.WriteLine("Error - Invalid menu item"); DisplayMenu(); menuItem = char.ToUpper(char.Parse(Console.ReadLine())); } } 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(); }推荐答案static ShotgunOption GetOptionFromUser() { char menuItem; do { DisplayMenu(); menuItem = char.ToUpper(char.Parse(Console.ReadLine())); if (menuItem == 'S') { return ShotgunOption.Shoot; } else if (menuItem == 'P') { return ShotgunOption.Shield; } else if (menuItem == 'R') { return ShotgunOption.Reload; }else {Console.WriteLine("Error - Invalid menu item");}while (menuItem != 'F' && menuItem != 'C' && menuItem != 'I' && menuItem != 'X' && menuItem != 'R' && menuItem != 'D') } } 我没有触及条件,因为他们与菜单无关,所以也许你有一些隐藏的应用程序逻辑...稍微玩一下。 祝你好运。I didn't touch the conditions because they do not relate to the menu so maybe you have some more application logic hidden...play with it for a bit.Good luck. 这篇关于返回方法中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!