我對C#很新穎(好吧,一般編程) 我正在嘗試爲c#控制檯應用程序創建菜單。從菜單中選擇後,菜單不斷重新出現...我已經研究並嘗試了許多不同的選項,但似乎沒有任何工作適合我......我知道這是我做錯了一些愚蠢的事情。 任何意見或指導將非常感激。先謝謝了。控制檯應用程序c#中的菜單問題
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);
}
那麼,depsite事實上,你用'3'退出,因此循環的實際情況變得無用,程序看起來很好,編譯和工作 - 至少對我而言。 – HimBromBeere
您編寫它使其重新出現(do循環)。根據你的功能做什麼,它會再次循環並打印菜單,直到你退出。 –
你應該使用'TryParse();'來避免異常。 – AntiHeadshot