[英]c# menu in console application
嗨,我正在嘗試制作一個指令菜單,我想獲取指令編號作為繼續的輸入。 這是我的代碼:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("please choose one of the numbers below : "+"\n");
Console.WriteLine("1.adding new student"+"\n");
Console.WriteLine("2.adding new course"+"\n");
Console.WriteLine("3.adding new grade"+"\n");
Console.WriteLine("4.showing the best student"+"\n");
Console.WriteLine("5.getting students average"+"\n");
Console.WriteLine("5.exit"+"\n");
Console.ReadKey();
if(ConsoleKey="1")
{
}
Console.ReadKey();
}
例如,如果用戶選擇數字 1,我該如何進行下一步? 因為我知道為 Console.Readkey() 分配一個數字是錯誤的
您這樣做是錯誤的,您沒有存儲函數調用返回的值,這就是您根本無法比較它的值的原因。 其次,正如 David 所提到的,您在 C# 中使用==
而不是=
,后者是一個賦值運算符。 考慮使用這個,
// For this approach, you would have to limit the values to one character.
if(Console.ReadLine() == "1")
{
// 1 was pressed
}
否則使用以下代碼,
if(Console.ReadKey().Key == ConsoleKey.D1)
{
// 1 was pressed
}
一個常見的例子是在程序末尾使用Console.ReadKey()
,防止終端關閉,並記住數據丟失。 您始終可以比較這些值並查看輸入了哪個值,有關更多信息,請參閱以下內容,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.