如何简化这段代码?

Pet*_*erP 1 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;
    }
Run Code Online (Sandbox Code Playgroud)

Man*_*lia 5

我想这会对你有所帮助......

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];
Run Code Online (Sandbox Code Playgroud)