简体   繁体   English

使用基于按下的键的菜单制作基本控制台 C# 应用程序

[英]Making a basic console c# application with a menu based on the key that is pressed

I'm trying to make a program which has two options based on the key is pressed by the user, it'will execute a different action.我正在尝试根据用户按下的键制作一个具有两个选项的程序,它将执行不同的操作。 The second option has to do to displaying some information about operations performed in the first option.第二个选项必须显示有关第一个选项中执行的操作的一些信息。 The main point is to try to make a navigation menu that the user can come back to the main menu through pressing "ESC" or "Enter" to continue in whatever he chooses重点是尝试制作一个导航菜单,用户可以通过按“ESC”或“Enter”返回主菜单以继续他选择的任何内容

Console.WriteLine("\nCAMPAIGN 2022\nSelect what you want to consult:\n1) Votin Urn\n2) DataBase"); // to make clear, database will display info about votes quantity
        int options = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("----------------------");
        if (options == 1)
        {
            ConsoleKeyInfo menu;
            Console.WriteLine("\nVOTING URN");
            while (exit == false)
            {
                Console.WriteLine("\nSelect your vote:\n1) Jair Bolsonaro;\n2) Luiz 
                Inacio Lula da Silva;\n3) White;\n4) NulL");
                vote = Convert.ToInt32(Console.ReadLine());
                while (vote != 1 & voto != 2 & vote!= 3 & vote != 4)
                {  
                    if (increment == 0)
                    {
                        Console.WriteLine("\nInvalid Vote. Try again:");
                    }
                    vote = Convert.ToInt32(Console.ReadLine());
                    increment += 1;

The thing is: i'm using an if statement and because of local scope when the user selects option 2, to know who have more votes for example, the value is gonna be 0. I tried using switch but it's the same thing.问题是:我正在使用 if 语句,并且由于用户选择选项 2 时的本地范围,例如,要知道谁有更多选票,该值将为 0。我尝试使用 switch,但它是同一件事。 What can I do?我能做些什么?

Also, when the user press "ESC" to come back to select "Voting Urn" or "Database" the first letter of the text displayed is cut, like instead of "Campaign" it's "ampaing".此外,当用户按“ESC”返回选择“Voting Urn”或“Database”时,显示的文本的第一个字母会被剪切,就像“ampaing”而不是“Campaign”一样。 I'm using the ConsoleKeyInfo:我正在使用 ConsoleKeyInfo:

Console.WriteLine("Press \"Enter\" to continue or \"Escape\" to return to the main menu");
                menu = Console.ReadKey();
                if (menu.Key == ConsoleKey.Escape)
                {
                    exit = true;
                }
                if (menu.Key == ConsoleKey.Enter)
                {
                    exit = false;
                }

I would put the while loop so it covers the menu aswell and then just add another choice that is "Go back to main menu / exit application" in a swtich statement.我会放置 while 循环,以便它也覆盖菜单,然后在 swtich 语句中添加另一个选项,即“返回主菜单/退出应用程序”。 And then have everything inside that switch statement and maybe do some fucnctions like TotalVotes() and Vote().Then do create variable names with each person you can vote for and just ++ each variable inside an if statement if they get a vote.然后将所有内容都包含在该 switch 语句中,并可能执行一些功能,例如 TotalVotes() 和 Vote()。然后为您可以投票的每个人创建变量名,如果他们获得投票,只需在 if 语句中 ++ 每个变量。 Also this is probably a typo?这也可能是一个错字? "voto != 2". “投票!= 2”。 It seems like your structure is making it harder that it has to for you.看起来你的结构让你变得更难了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM