首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将C#开关语句替换为更易读/更直观的格式

将C#开关语句替换为更易读/更直观的格式
EN

Stack Overflow用户
提问于 2022-02-20 01:40:26
回答 1查看 66关注 0票数 1

我希望为我正在创建的战舰程序清理一下我的代码。有一个2D阵列游戏板,它保存与在板上发现的每一艘船只有关的字符值,即游戏板上的一艘战舰将被表示为“BBBB”。

程序已经运行,但是检查用户所选择的坐标是否包含与船只相关的char值的部分似乎很混乱,而且我不知道如何使它看起来更干净。在此之前,它被实现为一堆if either语句,而且看起来也不太干净。任何帮助或指点来引导我朝着正确的方向前进是很棒的。谢谢。

代码语言:javascript
复制
            switch (board.Grid[userSelectedRow, userSelectedCol])
            {
                case 'C':
                    carrier_C_HitCounter++;
                    hitCounter++;
                    if (carrier_C_HitCounter != shipList[0].Length)
                    {
                        Console.WriteLine("You struck a ship!\n");
                    }
                    else
                        Console.WriteLine("You sunk their carrier!\n");
                    break;
                case 'B':
                    battleship_HitCounter++;
                    hitCounter++;
                    if (battleship_HitCounter != shipList[1].Length)
                    {
                        Console.WriteLine("You struck a ship!\n");
                    }
                    else
                        Console.WriteLine("You sunk their battleship!\n");
                    break;
                case 'S':
                    submarine_S_HitCounter++;
                    hitCounter++;
                    if (submarine_S_HitCounter != shipList[2].Length)
                    {
                        Console.WriteLine("You struck a ship!");
                    }
                    else
                        Console.WriteLine("You sunk their submarine!");
                    break;
                case 's':
                    submarine_s_HitCounter++;
                    hitCounter++;
                    if (submarine_s_HitCounter != shipList[3].Length)
                    {
                        Console.WriteLine("You struck a ship!");
                    }
                    else
                        Console.WriteLine("You sunk their submarine!");
                    break;
                case 'D':
                    destroyer_D_HitCounter++;
                    hitCounter++;
                    if (destroyer_D_HitCounter != shipList[4].Length)
                    {
                        Console.WriteLine("You struck a ship!");
                    }
                    else
                        Console.WriteLine("You sunk their destroyer!");
                    break;
                case 'd':
                    destroyer_d_HitCounter++;
                    hitCounter++;
                    if (destroyer_d_HitCounter != shipList[5].Length)
                    {
                        Console.WriteLine("You struck a ship!");
                    }
                    else
                        Console.WriteLine("You sunk their destroyer!");
                    break;
                default:
                    Console.WriteLine("No ships were hit.");
                    break;
            }
            // Change the hit location to 'X'
            board.SetChar(userSelectedRow, userSelectedCol, 'X');
EN

回答 1

Stack Overflow用户

发布于 2022-02-20 02:06:17

重复次数最多的代码是检查它是什么船,并生成相关消息:

代码语言:javascript
复制
if (carrier_C_HitCounter != shipList[0].Length)
{
     Console.WriteLine("You struck a ship!\n");
}
else
     Console.WriteLine("You sunk their carrier!\n");

您可以定义一个枚举和一个函数来生成类似于这样的消息,以稍微清理一下switch语句:

代码语言:javascript
复制
enum Ship
{
    "carrier",
    "battleship",
    "submarine",
    "destroyer"
}


public void getMessage(int number, int counter){
    if (counter != shipList[number].Length)
    {
         Console.WriteLine("You struck a ship!");
    }
    else
         Console.WriteLine("You sunk their " + (Ship)number + " !");
}

然后从开关中调用它,就像:

代码语言:javascript
复制
switch (board.Grid[userSelectedRow, userSelectedCol])
{
case 'C':
    carrier_C_HitCounter++;
    hitCounter++;
    getMessage(0,carrier_C_HitCounter);
    break;
case 'B':
    battleship_HitCounter++;
    hitCounter++;
    getMessage(1,battleship_HitCounter);
    break;
case 'S':
    submarine_S_HitCounter++;
    hitCounter++;
    getMessage(2,submarine_S_HitCounter);
    break;
case 's':
    submarine_s_HitCounter++;
    hitCounter++;
    getMessage(2,submarine_s_HitCounter);
    break;
case 'D':
    destroyer_D_HitCounter++;
    hitCounter++;
    getMessage(3,destroyer_D_HitCounter);
    break;
case 'd':
    destroyer_d_HitCounter++;
    hitCounter++;
    getMessage(3,destroyer_d_HitCounter);
    break;
default:
    Console.WriteLine("No ships were hit.");
    break;
}
// Change the hit location to 'X'
board.SetChar(userSelectedRow, userSelectedCol, 'X');

我假设shipList是全局的,但在其他情况下也会修改函数头以传递它。让您的代码看起来更干净的最重要的事情是查找大量的重复块,并试图找到一种“重新路由”它的方法。希望这对德里克有帮助:)欢迎来到这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71190719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档