简体   繁体   English

如何返回菜单?

[英]How do I go back to my menu?

 static void Main(string[] args)
    {

        Console.WriteLine("************************************************");
        Console.WriteLine("*                CAESAR CIPHER                 *");
        Console.WriteLine("*                -------------                 *");
        Console.WriteLine("*    1) Encrypt plain text                     *");
        Console.WriteLine("*    2) Decrypt plain text                     *");
        Console.WriteLine("*    3) Quit                                   *");
        Console.WriteLine("************************************************");

        Console.Write("Enter your option : ");
        string num = Console.ReadLine();
        int cnum = Convert.ToInt32(num);

        Console.WriteLine();
        string n = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string m = "DEFGHIJKLMNOPQRSTUVWXYZABC";

        if ( cnum==1)
        {
        Console.Write("Enter a string : ");
        string text = Console.ReadLine();

        Console.WriteLine();
        Console.WriteLine("Applying Caesar cipher ...");
        Console.WriteLine();
        text = text.Replace(" ", "");
        text = text.ToUpper();
        Console.WriteLine("Input Text = " + text);
        text = text.ToUpper();
        Console.Write("Output Text = ");
        text = text.Replace(" ", "");
        foreach (char i in text)
        {
            if (i >= 'A' && i <= 'Z')
            {
                int pos = n.IndexOf(i);
                Console.Write(m[pos]);

            }
            else
            {
                Console.Write(i);

            }

        }
            Console.WriteLine();
            Console.WriteLine("Press any key to return to menu");

        }
        else  if (cnum == 2)
        {
            Console.Write("Enter a string : ");
            string text = Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("Applying Caesar cipher ...");
            Console.WriteLine();
            text = text.Replace(" ", "");
            text = text.ToUpper();
            Console.WriteLine("Input Text = " + text);
            text = text.ToUpper();
            Console.Write("Output Text = ");
            text = text.Replace(" ", "");
            foreach (char i in text)
            {
                if (i >= 'A' && i <= 'Z')
                {
                    int pos = m.IndexOf(i);
                    Console.Write(n[pos]);
                }
                else
                {
                    Console.Write(i);

                }

            }
            Console.WriteLine();
            Console.WriteLine("Press any key to return to menu");
        }

        else 
        {
            Console.WriteLine("Bye!");
        }
            Console.ReadKey();
    }
}
}

This is my code. 这是我的代码。 How do I go back to my menu? 如何返回菜单? I tried goto statement but it immediately went back to my menu. 我尝试了goto语句,但它立即返回菜单。 I want to press any key for it to go back to my menu statement. 我想按任意键返回到我的菜单语句。 I am not sure how to do it so can somehow help me out here? 我不确定该怎么做,所以可以在某种程度上帮助我吗? Can I not use the switch statement and still do it using if else statements? 我可以不使用switch语句,而仍然使用if else语句吗?

Just wrap all your code in a while (true) block to repeat it over and over again. 只需将所有代码包装在while (true)块中,即可一遍又一遍地重复。 To jump out of it, use break when the user enters 3 : 要跳出它,请在用户输入3时使用break

static void Main(string[] args)
{
    while (true)
    {
        // your existing code

        if (cnum == 1)
        {
            // your existing code
        }
        else if (cnum == 2)
        {
            // your existing code
        }
        else
        {
            Console.WriteLine("Bye!");
            break;
        }
        Console.ReadKey();
    }
}

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

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