我该如何回到我的菜单?

时间:2015-11-22 06:51:59

标签: c# if-statement console

 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();
    }
}
}

这是我的代码。我该如何回到我的菜单?我尝试了goto声明,但它立即回到我的菜单。我想按任意键返回我的菜单语句。我不知道怎么做才能以某种方式帮助我在这里?我可以不使用switch语句,仍然使用if else语句吗?

1 个答案:

答案 0 :(得分:1)

将所有代码包装在while (true)块中,一遍又一遍地重复。要跳出来,请在用户输入break时使用3

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();
    }
}