C# 在用户找到正确的路径之前,问他们相同问题的最佳方式是什么?

C# 在用户找到正确的路径之前,问他们相同问题的最佳方式是什么?,c#,recursion,C#,Recursion,我有一个由地图构建的2D数组,它向用户询问坐标x和y。我只是想不断地询问用户坐标,直到他们达到目标 我想知道使用switch/case语句是否比使用多个if/else-if语句更好。我尝试编写一个名为CoordinateInput的单独方法,将输入指令封装到用户,然后返回坐标!='G'。不幸的是,这没有奏效 char[,] map = new char[6,6] {{ 'W', 'W', 'W', 'W', 'W', 'W' },

我有一个由地图构建的2D数组,它向用户询问坐标x和y。我只是想不断地询问用户坐标,直到他们达到目标

我想知道使用switch/case语句是否比使用多个if/else-if语句更好。我尝试编写一个名为CoordinateInput的单独方法,将输入指令封装到用户,然后返回坐标!='G'。不幸的是,这没有奏效

char[,] map = new char[6,6]   {{ 'W', 'W', 'W', 'W', 'W', 'W' },
                               { 'W', 'S', ' ', ' ', ' ', 'W' },
                               { 'W', 'E', 'W', ' ', 'E', 'W' },
                               { 'W', ' ', ' ', ' ', ' ', 'W' },
                               { 'W', ' ', 'W', ' ', 'G', 'W' },
                               { 'W', 'W', 'W', 'W', 'W', 'W' }};
for (int y = 0; y < 6; y++)
{
    for (int x = 0; x < 6; x++)
    {
        Console.Write(map[x, y] + " ");
    }
    Console.WriteLine();
}
Console.WriteLine("Please enter an x coordinate");
int xC = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter a y coordinate");
int yC = int.Parse(Console.ReadLine());
char coordinate = map[yC,xC];
map[yC, xC] = coordinate;

if (coordinate == ' ')
{
    Console.WriteLine("You are on free block");                
}
else if (coordinate == 'W')
{
    Console.WriteLine("You are on a Wall");                
}
else if (coordinate == 'E')
{
    Console.WriteLine("You recieved an Energy Boost");                
}
else if (coordinate == 'S')
{
    Console.WriteLine("You the starting place");                
}         
else if (coordinate == 'G')
{
    Console.WriteLine("You Win!! You reached the goal!!");
}

这开始只是一个评论,但它变得太大了,所以

请注意,此代码中还有其他问题:

首先,您正在对Console.ReadLine中的值使用int.Parse-没有任何东西可以阻止用户输入像Conley或Banana这样的值,这将引发FormatException并破坏游戏

第二,即使用户输入一个数字,如果该数字要么太大,要么太大,它仍然可能导致异常-这将是一个IndexOutOfBoundException

第三,你似乎在用一种方法来做整个事情——违反了单一责任原则

第四,您将逻辑与用户交互相结合。编写一个简单的控制台应用程序并没有那么糟糕,但这是一种不好的做法,您应该避免这种做法,以便尽早了解最佳做法

第五,使用硬编码值绘制地图。这意味着,如果更改贴图,还必须更改循环。改用GetLength方法

因此,可以通过使用不同的方法从用户处读取坐标来处理第一个、第二个和第三个-类似这样的方法可以满足您的需要:

static int ReadCoordinate(string message, int maxValue)
{
    Console.WriteLine(message);
    var isValid = false;
    int value;
    do
    {
        isValid = int.TryParse(Console.ReadLine(), out value) && value > 0 && value <= maxValue;
        if (!isValid)
        {
            Console.WriteLine("Please try again.");
        }
    } while (!isValid);
    return value;
}
然后,您的主要方法可以简单地如下所示:

static void Main(string[] args)
{

    char[,] map = new char[6, 6]   {{ 'W', 'W', 'W', 'W', 'W', 'W' },
                                    { 'W', 'S', ' ', ' ', ' ', 'W' },
                                    { 'W', 'E', 'W', ' ', 'E', 'W' },
                                    { 'W', ' ', ' ', ' ', ' ', 'W' },
                                    { 'W', ' ', 'W', ' ', 'G', 'W' },
                                    { 'W', 'W', 'W', 'W', 'W', 'W' }};

    DrawMap(map);
    GamePlay(map);
}

快点!userHasWon@CodeCaster这应该是答案,再加上一些文字,比如你想在用户还没有赢的时候或者在用户赢之前做点什么,因此你可以写:@Rafalon这肯定是重复的,但我找不到那么快的答案,因此没有完整的答案。再想一想,这是一个两个问题合一的问题,OP询问如何循环直到[condition],以及ifs/else ifs和Switch之间有什么更好的方法感谢您的回答,我现在正在试验do/while循环。堆栈溢出是一个轻描淡写。你提到的各种问题,但除了第五,不提供任何解决方案,更重要的是,不回答主要问题。顺便说一句,为了方便地从控制台读取int和其他类型的值,请查看我的名为ExtendedConsole的git hub项目。哇,这非常有帮助。从长远来看,这确实有助于我创建更好的代码。非常感谢你!!
static void GamePlay(char[,] map)
{
    bool gameOver = false;
    do
    {

        var x = ReadCoordinate("Please enter an x coordinate", map.GetLength(0) - 1);
        var y = ReadCoordinate("Please enter an y coordinate", map.GetLength(1) - 1);

        var value = map[x, y];
        var response = "";
        switch (value)
        {
            case ' ':
                response = "You are on free block";
                break;
            case 'W':
                response = "You are on a Wall";
                break;
            case 'E':
                response = "You recieved an Energy Boost";
                break;
            case 'S':
                response = "You the starting place";
                break;
            case 'G':
                response = "You Win!! You reached the goal!!";
                gameOver = true;
                break;
        }
        Console.WriteLine(response);
    } while (!gameOver);
}
static void Main(string[] args)
{

    char[,] map = new char[6, 6]   {{ 'W', 'W', 'W', 'W', 'W', 'W' },
                                    { 'W', 'S', ' ', ' ', ' ', 'W' },
                                    { 'W', 'E', 'W', ' ', 'E', 'W' },
                                    { 'W', ' ', ' ', ' ', ' ', 'W' },
                                    { 'W', ' ', 'W', ' ', 'G', 'W' },
                                    { 'W', 'W', 'W', 'W', 'W', 'W' }};

    DrawMap(map);
    GamePlay(map);
}