命令后如何重新启动代码?

时间:2019-03-23 18:51:48

标签: c# console-application

我需要在正在制作的游戏中划分3个区域,并且需要重置代码,以便您可以在区域之间移动,是否有可以执行此操作的命令或代码?

要离开该区域后,需要在一个窗口上完成

if (choice == 2)
{
    Console.WriteLine("you live");
}

Console.WriteLine("you left the temple and went towards the forest");
// the code starts over form the beginning

我希望它在区域结束后重新启动代码。

1 个答案:

答案 0 :(得分:1)

我建议在 while 循环内使用 switch 案例,但是下面我添加了一个简单的 if 语句的可能解决方案(或 else if (如果愿意)。

while (true) 
{
if (choice == 2) 
{
    Console.WriteLine("you live");
}
//Have a specific number for the user to input when they want to exit the program
if (choice == 3) 
{
    //break from the while loop
    break;
}
Console.WriteLine("you left the temple and went towards the forest");
// the code starts over form the beginning
}
相关问题