我有一个主菜单,要求用户输入一个数字,对于每个数字,在switch语句中都有一个case 我想要做的是,当用户输入一个没有大小写的数字时,它应该再次返回相同的菜单
所以我创建了一个包含switch语句的函数,它的默认值是函数本身,但它不起作用(不再显示该函数) 我该怎么办?
static void AdminMenu()
{
char innerchoice;
Console.WriteLine("Press [1] to Add new student.");
Console.WriteLine("Press [2] to Add new course.");
Console.WriteLine("Press [3] to Enter course prerequisite.");
Console.WriteLine("Press [4] to View List of all students in a specific course.");
Console.WriteLine("Press [5] to View List of Finished courses.");
Console.WriteLine("Press [6] to View List of courses in progress.");
Console.WriteLine("Press [7] to edit all course data.");
innerchoice = char.Parse(Console.ReadLine());
switch (innerchoice)
{
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
break;
case '7':
break;
default:
AdminMenu();
}
}
答案 0 :(得分:2)
不要使用递归来实现用户菜单。它会使您的代码变得混乱且难以调试。只需使用任何循环并检查它是否应该再次运行:
void PrintMenu()
{
Console.WriteLine("Press [1] to Add new student.");
Console.WriteLine("Press [2] to Add new course.");
Console.WriteLine("Press [3] to Enter course prerequisite.");
Console.WriteLine("Press [4] to View List of all students in a specific course.");
Console.WriteLine("Press [5] to View List of Finished courses.");
Console.WriteLine("Press [6] to View List of courses in progress.");
Console.WriteLine("Press [7] to edit all course data.");
}
static void AdminMenu()
{
bool run = true;
while(run)
{
PrintMenu();
char innerchoice = char.Parse(Console.ReadLine());
switch (innerchoice)
{
case '1':
run = false;
break;
case '2':
run = false;
break;
case '3':
run = false;
break;
case '4':
run = false;
break;
case '5':
run = false;
break;
case '6':
run = false;
break;
case '7':
run = false;
break;
}
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
您必须在默认情况下添加break
,否则会出错。
试用此代码:
static void AdminMenu()
{
char innerchoice;
Console.WriteLine("Press [1] to Add new student.");
Console.WriteLine("Press [2] to Add new course.");
Console.WriteLine("Press [3] to Enter course prerequisite.");
Console.WriteLine("Press [4] to View List of all students in a specific course.");
Console.WriteLine("Press [5] to View List of Finished courses.");
Console.WriteLine("Press [6] to View List of courses in progress.");
Console.WriteLine("Press [7] to edit all course data.");
innerchoice = char.Parse(Console.ReadLine());
switch (innerchoice)
{
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
break;
case '7':
break;
default:
AdminMenu();
break;// add break in default
}
}
输出
答案 3 :(得分:0)
您可以使用bool来控制值是否有效并将其置于循环
试用此代码:
static void AdminMenu()
{
char innerchoice;
bool Leave = false;
while (Leave == false)
{
Leave = true;
innerchoice = GetChoice();
switch (innerchoice)
{
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
break;
case '7':
break;
default:
Leave = false;
Console.Clear();
Console.WriteLine("Press a valid value.");
break;
}
}
}
static char GetChoice()
{
Console.WriteLine("Press [1] to Add new student.");
Console.WriteLine("Press [2] to Add new course.");
Console.WriteLine("Press [3] to Enter course prerequisite.");
Console.WriteLine("Press [4] to View List of all students in a specific course.");
Console.WriteLine("Press [5] to View List of Finished courses.");
Console.WriteLine("Press [6] to View List of courses in progress.");
Console.WriteLine("Press [7] to edit all course data.");
return char.Parse(Console.ReadLine());
}
这是按下无效值时所做的output。