我对C#很有新意(一周前开始学习它),在批处理和表达式2方面有一些经验,并且我一直在尝试基于文本的游戏,学到更多。起初我使用了goto语句,但根据我发现的几乎所有人,goto语句都是一些死亡和绝望的混合物,所以我想学习更清洁,更少的邪恶方法来达到同样的效果。下面是我做证明我是什么意思一个糟糕的示例脚本:goto语句的替代方法
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
的一种方式回到以前的声明,以浏览菜单并可能重复基于回合的游戏的算法。我在Alternative to using goto statement in C#(这基本上与我的问题基本相同,除了更模糊一些)之外,我已阅读了有关使用此方法的内容,但Greg做出的示例根本不适用于我,因此可能不值得尝试使这个特定的例子有效。
我认为这个问题是一个更合适的网站是一个网站http://codereview.stackexchange.com/ – BWA
是啊,不要使用goto语句,永远 – Liam
@Liam该帖子的全部内容是不使用goto声明,永远。 –