C# 如何仅在另一个方法已运行时执行该方法?
我有一个选择的用户在主要使用开关。根据用户的选择,稍后会有几个选择,程序将结束或不结束。至少,这是我正在努力实现的目标C# 如何仅在另一个方法已运行时执行该方法?,c#,methods,switch-statement,C#,Methods,Switch Statement,我有一个选择的用户在主要使用开关。根据用户的选择,稍后会有几个选择,程序将结束或不结束。至少,这是我正在努力实现的目标 //This is in Main string[] menyVal2 = new string[] {"Go to the hotel room", "Go to the dining hall"}; string title2 = "text"; int choice2 = menu(menuChoice2, title2);
//This is in Main
string[] menyVal2 = new string[] {"Go to the hotel room", "Go to the dining hall"};
string title2 = "text";
int choice2 = menu(menuChoice2, title2);
switch (choice2)
{
case 0:
hotelRoom();
break;
case 1:
diningHall();
break;
}
许多行代码之后
public static void save()
{
Console.Clear();
string[] menuChoice = {"Chapter 2", "Back to start"};
string title = "text";
int choice = menu(menuChoice, title);
switch (val)
{
case 0:
if (hotelRoom = true) //this if-statement does not work
{
withoutCross();
}
else if (diningHall = true)
{
withCross();
}
break;
case 1:
Main(new string[] { });
break;
}
}
如果我正确理解了问题的标题,那么这就是一个解决方案: 将方法的返回类型设为bool,并将其填充到变量中
bool isMethodExecuted = MyMethod();
稍后,如果执行了该方法,则可以使用if语句进行检查:
if(isMethodExecuted)
MyOtherMethod();
那么问题是什么呢?如果我正确理解了问题的标题,那么这就是一个解决方案:创建方法bool的返回类型并将其填充到变量。稍后您可以检查变量是否为true。然后执行另一个方法。那么您将哪个代码称为停止或不停止的开关?