简体   繁体   English

c#中的控制台应用程序菜单问题

[英]Console application Menu issues in c#

i'm very new to C# (well, programming in general) I am trying to create a menu for ac# console application.我对 C# 很陌生(嗯,一般编程)我正在尝试为 ac# 控制台应用程序创建一个菜单。 The the menu keeps reappearing after a selection from the menu is made... I've researched and tried many different options but nothing seems to be working for me.... I know it's something stupid that I've done incorrectly.从菜单中进行选择后,菜单不断重新出现...我研究并尝试了许多不同的选项,但似乎没有任何效果对我有用....我知道我做错了一些愚蠢的事情。 Any advice or guidance would be very much appreciated.任何建议或指导将不胜感激。 Thanks in advanced.提前致谢。

static void Main()  //Start of program
{   
    //Menu and other UI stuff

    int userSelection = 0;

    do
    {
        Console.WriteLine("[1]  Encryption");
        Console.WriteLine("[2]  Decryption");
        Console.WriteLine("[3]  Exit");

        Console.Write ("Please choose an option 1-3:  ");
        userSelection = Int32.Parse(Console.ReadLine()); 

        switch(userSelection)
        {   
            case 1:
                readFile();
                break;

            case 2:
                decryption();
                break;

            case 3:
                Environment.Exit(0);
                break;
            default:
                Console.WriteLine("Your selection is invalid. Please     try again.");
                break;
        }
    }
    while (userSelection != 4);
}

Your do/while only will stops when your userSelection have value 4, in this example, it never will happen.你的 do/while 只会在你的 userSelection 值为 4 时停止,在这个例子中,它永远不会发生。

change your while condition to将您的 while 条件更改为

while(userSelection <= 0 || userSelection > 3)

it should solve...应该可以解决...

maybe you would like to use something like:也许你想使用类似的东西:

int userSelection = 0;
bool validAnswer = false;
do
{
    Console.WriteLine("[1]  Encryption");
    Console.WriteLine("[2]  Decryption");
    Console.WriteLine("[3]  Exit");

    Console.Write ("Please choose an option 1-3:  ");
    userSelection = Int32.Parse(Console.ReadLine()); 

    switch(userSelection)
    {   
        case 1:
            readFile();
            validAnswer = true;
            break;

        case 2:
            decryption();
            validAnswer = true;
            break;

        case 3:
            validAnswer = true;
            Environment.Exit(0);
            break;
        default:
            Console.Clear();
            Console.WriteLine("Your selection is invalid. Please     try again.");
            break;
    }

}while (!validAnswer);

It keeps reappearing because you placed your code in a do while loop.它不断重新出现,因为您将代码置于do while循环中。 If you want to run this code only once don't use looping constructs, just place it directly in Main .如果您只想运行此代码一次,不要使用循环结构,只需将其直接放在Main

If you use something like如果你使用类似的东西

do
{
    // ...
}
while (userSelection != 4);

the code inside the loop will be repeated until the user enters 4 .循环内的代码将重复,直到用户输入4

From msdn article on a do while :来自 msdn 文章do while

The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. do 语句重复执行一个语句或一个语句块,直到指定表达式的计算结果为 false。

Another option would be to use a break statement after the switch block.另一种选择是在switch块之后使用break语句。

class Program
{
    static void Main()  //Start of program
    {
        //Menu and other UI stuff

        string userSelection;

        do
        {
            Console.Clear();
            Console.WriteLine("[1]  Encryption");
            Console.WriteLine("[2]  Decryption");
            Console.WriteLine("[3]  Exit");

            Console.Write("Please choose an option 1-3:  ");
            userSelection = Console.ReadLine();

            switch (userSelection)
            {
                case "1":
                    Console.WriteLine("mission 1");
                    break;

                case "2":
                    Console.WriteLine("mission 2");
                    break;

                case "3":
                    Environment.Exit(0);
                    break;
                default:
                    Console.WriteLine("Your selection is invalid. Please     try again.");
                    break;
            }
            Console.ReadLine();
        }
        while (true);
    }
}

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

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