c# - 如何简化这段代码?

标签 c# visual-studio-2010

<分区>

你知道如何简化这个简单的“翻译机制”吗?

哈希表有用吗?

    char translateChar(char strIn)
    {
        char strOut = '?';

        if (strIn == 'A') strOut = '1';
        else if (strIn == 'B') strOut = '2';
        else if (strIn == 'C') strOut = '3';
        else if (strIn == 'D') strOut = '4';
        else if (strIn == 'E') strOut = '5';
        else if (strIn == 'F') strOut = '6';
        else if (strIn == 'G') strOut = '7';
        else if (strIn == 'H') strOut = '8';
        else if (strIn == 'I') strOut = '9';
        else if (strIn == 'J') strOut = '@';
        else if (strIn == 'K') strOut = 'A';
        else if (strIn == 'L') strOut = 'B';
        else if (strIn == 'M') strOut = 'C';
        else if (strIn == 'N') strOut = 'D';
        else if (strIn == 'O') strOut = 'E';
        else if (strIn == 'P') strOut = 'F';
        else if (strIn == 'Q') strOut = 'G';
        else if (strIn == 'R') strOut = 'H';
        else if (strIn == 'S') strOut = 'I';
        else if (strIn == 'T') strOut = 'J';
        else if (strIn == 'U') strOut = 'K';
        else if (strIn == 'V') strOut = 'L';
        else if (strIn == 'W') strOut = 'M';
        else if (strIn == 'X') strOut = 'N';
        else if (strIn == 'Y') strOut = 'O';
        else if (strIn == 'Z') strOut = 'P';
        else if (strIn == '2') strOut = 'X';
        else if (strIn == '1') strOut = 'Y';
        else if (strIn == '_') strOut = '_';

        return strOut;
    }

最佳答案

我想这对你有帮助...

char[] strIN = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '1', '_' };
        char[] strOut = { '2', '3', '4', '5', '6', '7', '8', '9', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'X', 'Y', '_' };


        char init = 'C';

        int index = Array.IndexOf(strIN, init);
        char output = strOut[index];

关于c# - 如何简化这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590621/

相关文章:

c# - 获取我的实际 DNS

c# - 会调用什么函数?

c# - 在电子邮件中编码为 UTF-8

visual-studio-2010 - 在 Windows x64 上安装 node-canvas 平台 x64 无效?

visual-studio-2010 - 调试使用 Microsoft Azure 服务的 Windows CE 应用程序

visual-studio-2010 - 单击按钮打开新表单,关闭现有表单,VS2010

c# - 如何传递mysql查询和连接字符串并获取结果?

c# - 如何有条件地隐藏 WPF DataGrid 的 TemplateColumn 中的元素?

c# - 如何启动 Windows 窗体应用程序?

c# - 无法加载 DLL ' mydll.dll' : The specified module could not be found