[英]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();
}
}
}
這是我的代碼。 如何返回菜單? 我嘗試了goto語句,但它立即返回菜單。 我想按任意鍵返回到我的菜單語句。 我不確定該怎么做,所以可以在某種程度上幫助我嗎? 我可以不使用switch語句,而仍然使用if else語句嗎?
只需將所有代碼包裝在while (true)
塊中,即可一遍又一遍地重復。 要跳出它,請在用戶輸入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.