[英]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.