How to implement a console menu having submenus in C#(如何在 C# 中实现具有子菜单的控制台菜单)
问题描述
(C#) 我正在开发一个类似于 RedBox 的程序,它将具有客户和经理功能.我只是想在开始实现更多功能之前让所有菜单正常工作,但我遇到了一个我似乎无法理解的错误.我的问题似乎出在 CustomerMenu() 方法和 MainMenu() 方法中.我已经添加了我拥有的所有内容,以便您可以获得完整的图片.感谢任何帮助,因为我仍然有点新,所以感谢任何提示,谢谢.
(C#) I am working on a program that is similar to RedBox which will have customer and manager functions. I am just trying to get all the menu's working correctly before I begin implementing more functions, but I am encountering an error that I can not seem to understand. My issue seems to be in the CustomerMenu() method and in the MainMenu() method. I have added all that I have so you can get the full picture. Any help is appreciated as I am still somewhat new so any tips are appreciated, thank you.
推荐答案
使用平面菜单系统
您可以尝试更正并稍微重构一下.
You can try this corrected and a little refactored.
我们创建了一个获取用户选择的方法,因此不再需要重复代码.我们使用 uint 是因为选择是肯定的,并且使用 TryParse 来转换输入的字符串.出错时返回 0,所以这里没问题.
We created a method to get the user choice, so repeat code is no more needed. We use uint because choice is positive and TryParse to convert the inputed string. It returns 0 in case of error, so that's fine here.
我们还使用 lamda 来打印选择字符串以不重复它们.
Also we use a lamda to print the choices strings to not repeat them.
接下来我们使用 switch 来管理选择,以便代码更干净和可维护.
Next we use a switch to manage choice so the code is more clean and maintainable.
清除菜单之间的控制台,我们提供根菜单和子菜单之间的导航.
The console is cleared between menus and we offer navigation between root and sub menus.
未来的改进是使用运行这些表的自动菜单管理器创建菜单标题、选项和相关方法的表格.只是稍微复杂一点,但不要太多.这可以通过创建一些集合和一个 MenuManager 类来完成.有了这样的东西,你将拥有一个健壮的系统,代码很少,没有重复.
A future improvement is to create tables of menus headers, choices and associated methods... with a auto-menu manager that runs these tables. Just a little more complex but not too much. It can be done by creating some collections and a MenuManager class. With such thing, you will have a robust system with very few code and nothing repeated.
使用自动菜单管理器
这是菜单选项类:
这是菜单类:
这是菜单管理器类:
这里是菜单管理器的初始化,在选择中使用方法或另一个菜单而不是空值:
Here the menu manager initialization, using methods or another menu instead of null in choices:
现在要做的是:
这篇关于如何在 C# 中实现具有子菜单的控制台菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!