所以我正在为我的程序设计一个开关柜菜单,但是我有很多问题(我可能遗漏了一些非常明显的东西)
首先,我尝试实现一个while循环,以便在执行任何case方法后返回菜单。然而,当尝试实现while循环时,由于某种原因,它似乎无法识别我的bool变量。
其次,我不太确定如何让用户在做完他们想做的事情后,在他们选择的情况下返回开始菜单,这可能有一个很简单的解决方案,但我就是找不到一个。
[代码]
private string[] säten = new string[24];
private int Antal_passagerare = 0;
public void Run()
{
bool continue = true;
while(continue)
{
string menu = (Console.ReadLine());
int tal = Convert.ToInt32(menu);
switch(tal)
{
case 1:
Add_passagerare;
break;
case 2:
break;
case 3:
break;
}
}
}
[/code]
您的问题是本地变量名与控制循环流的C#关键字(或语句)
必须重命名局部变量。但是由于流控制关键字,您可以删除局部变量(见下文)。如果用户输入了非数字值,还可以使用
// Start an infinite loop. Use the break statement to leave it.
while (true)
{
string userInput = Console.ReadLine();
// Quit if user pressed 'q' or 'Q'
if (userInput.Equals("Q", StringComparison.OrdinalIgnoreCase)
{
// Leave the infinite loop
break;
}
// Check if input is valid e.g. numeric.
// If not show message and ask for new input
if (!(int.TryParse(userInput, out int numericInput))
{
Console.WriteLine("Only numbers allowed. Press 'q' to exit.");
// Skip remaining loop and continue from the beginning (ask for input)
continue;
}
switch (numericInput)
{
case 1:
break;
case 2:
Add_passagerare();
break;
case 3:
break;
}
}