C# goto语句的替代方案
我对C#(一周前开始学习)相当陌生,在batch and Expression 2方面有一点经验,我一直在开发一款基于文本的游戏,试图了解更多。我一开始使用goto语句,但据我发现的几乎所有人说,goto语句是死亡和绝望的混合体,所以我想学习更干净、更少邪恶的方法来达到同样的效果。下面是我制作的一个拙劣的示例脚本,用于演示我的意思:C# goto语句的替代方案,c#,goto,C#,Goto,我对C#(一周前开始学习)相当陌生,在batch and Expression 2方面有一点经验,我一直在开发一款基于文本的游戏,试图了解更多。我一开始使用goto语句,但据我发现的几乎所有人说,goto语句是死亡和绝望的混合体,所以我想学习更干净、更少邪恶的方法来达到同样的效果。下面是我制作的一个拙劣的示例脚本,用于演示我的意思: using System; namespace TestScript { class Program { public static void Main(s
using System;
namespace TestScript
{
class Program
{
public static void Main(string[] args)
{
string ConsoleReadinator;
string ConsoleReadinator2;
int Go = 0;
mainmenu:
do
{
Go = 0;
Console.Clear();
Console.WriteLine("Main Menu:");
Console.WriteLine("Store or something");
ConsoleReadinator = Console.ReadLine().ToUpper();
if (ConsoleReadinator == "STORE") { Go = 1; }
} while (Go == 0);
// In-game store example
{
Go = 0;
do
{
Console.Clear();
Console.WriteLine("In-game store I guess");
Console.WriteLine("Stuff you can buy, etc");
ConsoleReadinator2 = Console.ReadLine().ToUpper();
if (ConsoleReadinator2 == "GO") { Go = 1; }
} while (Go == 0);
goto mainmenu;
}
}
}
}
这个脚本是功能性的,但是我想避免使用
goto
作为返回到前面语句的一种方式,以便导航菜单,或者重复基于回合的游戏的算法。我在中读到了关于使用方法的内容(这基本上与我的问题相同,只是有点模糊),但Greg在那里做的示例对我来说根本不起作用,以至于可能不值得尝试让这个特定的示例起作用。通常使用方法或lambda表达式。因此,您的主菜单将成为一个在代码末尾再次调用的方法。据我所知,您需要一个无限循环:
一个建议是使用如下开关盒构造:
static void Main(string[] args)
{
string ConsoleReadinator;
string MENU_TEXT = "Main Menu:";
string ADDITIONAL_INFO = "Store or something";
bool endProg = false;
ConsoleReadinator = printMenu(MENU_TEXT, ADDITIONAL_INFO);
// as long "EXIT" is not typed
while (!endProg)
{
switch (ConsoleReadinator)
{
case "STORE":
// Do your Store Stuff
// maybe change MENU_TEXT and ADDITIONAL_INFO
// and print a new Menu
ConsoleReadinator = printMenu(MENU_TEXT, ADDITIONAL_INFO);
break;
case "GO":
// Do your Go Stuff
break;
case "EXIT":
endProg = true; // set exit condition to true
break;
default:
break;
}
}
Console.ReadKey();
}
// one Method to use for Menu display
public static string printMenu(string menuText, string additionalInfo)
{
Console.Clear();
Console.WriteLine(menuText);
Console.WriteLine(additionalInfo);
return Console.ReadLine().ToUpper();
}
您可以使用递归返回并再次执行代码。为此,您可以将代码移动到单独的方法中,并在必要时从其中调用它:
class Program
{
public static void Main(string[] args)
{
string ConsoleReadinator;
string ConsoleReadinator2;
Method(0);
}
private static void Method(int Go)
{
do
{
..
} while (Go == 0);
// In-game store example
do
{
...
} while (Go == 0);
Method(Go);
}
}
或者你可以用更合适的方式使用循环。让我们看一看希望用户输入整数的示例:
public static void Main(string[] args)
{
int num;
// This loop ends only when user enters proper integer number
do
{
Console.Clear();
Console.Write("Please enter some integer number: ");
} while(!int.TryParse(Console.ReadLine(), out num));
}
这可以通过递归的其他方式实现:
public static int EnterNumber()
{
Console.Clear();
Console.Write("Please enter some integer number: ");
// if number is successfully parsed return number else run method again
return int.TryParse(Console.ReadLine(), out num)?
num : EnterNumber();
}
public static void Main(string[] args)
{
int num = EnterNumber();
}
当我们可以使用方法、循环和递归时,就不再实际需要使用
GO TO
s了。我认为这个问题更合适的站点是一个站点是的,不要使用goto语句,ever@Liam这篇文章的重点是永远不要使用goto语句。我可能会被唤醒,但是经过大约20年的发展,DOS、Windows、C++、ASP、C等,我决定一个方法的Goto是简单的处理复杂代码中错误的好方法。我的反对者,C++高级工程师和可怕的聪明人同意。几分钟前我评论了这句话,它起作用了,它对这个例子来说,但我忽略了,有多种选择。不仅仅是一家商店。如果我使用这样一个无限循环,我想我必须循环浏览每个菜单,除非我使用了很多循环。@Rowan Tarshis:为什么不添加另一个菜单项,退出时只需调用break
(只留下上面的无限循环)或返回
(离开主功能和程序)?我不知道你的意思。“另一个菜单项”是指另一个菜单,比如商店,还是其他什么?此外,通过“Exit on which just callbreak;
(只留下上面的无限循环),”您的意思是它将break代码>进入第二个无限循环,或者我误解了吗?@Rowan Tarshis:是的,就像“Store”
,所以当用户打印时,说“Exit”,我们就离开了循环
public static int EnterNumber()
{
Console.Clear();
Console.Write("Please enter some integer number: ");
// if number is successfully parsed return number else run method again
return int.TryParse(Console.ReadLine(), out num)?
num : EnterNumber();
}
public static void Main(string[] args)
{
int num = EnterNumber();
}