菜单的 Switch 语句 (c#) [英] Switch statements for a Menu (c#)
本文介绍了菜单的 Switch 语句 (c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在为我的一个编程单元做作业,我需要使用 switch 语句来显示菜单并收集数据.我似乎已经计算出了 switch 语句本身,但我想知道一旦案例执行,我如何将用户返回到菜单?
I am currently doing an assignment for one of my programming units and I am required to use switch statements do display a menu and collect data. I seem to have worked out the switch statement itself, but I am wondering how I return the user to the menu once a case has executed?
例如:
Console.writeline("Enter Customer Details (s) ");
Console.writeline("Enter usage data (u) ");
Console.writeline("Display usage data (d) ");
menu = console.readline();
switch (menu)
case 's':
statement
break;
case 'u':
statement
break;
case 'd':
statement
break;
default :
statement
break;
现在,假设用户想先输入使用情况数据,我该如何将他们返回到菜单",以便他们可以选择输入客户详细信息和/或显示数据.
Now, let's say the user wants to enter the Usage data first, how do I return them to the 'menu' so they can opt to enter customer details and/or display the data.
推荐答案
正如我在评论中所说,您可以将其嵌入while
循环
As I said in the comments, you could embed it in a while
loop
bool continue = true;
while (continue)
{
Console.WriteLine("Enter Customer Details (s)");
Console.WriteLine("Enter usage data (u)");
Console.WriteLine("Display usage data (d)");
Console.WriteLine("Exit (e)");
string menu = Console.ReadLine();
switch (menu)
{
case "s":
//statement
break;
case "u":
//statement
break;
case "d":
//statement
break;
case "e":
default:
continue = false;
break;
}
}
这篇关于菜单的 Switch 语句 (c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文