我正在尝试构建需要加密和解密的Polybius密码。

因此,例如,我最初如何获得这个正方形中“世界”一词的坐标?

public static char[][] cypher = {
    {'p', 'h', '0', 'q', 'g', '6'},
    {'4', 'm', 'e', 'a', '1', 'y'},
    {'l', '2', 'n', 'o', 'f', 'd'},
    {'x', 'k', 'r', '3', 'c', 'v'},
    {'s', '5', 'c', 'w', '7', 'b'},
    {'j', '9', 'u', 't', 'i', '8'},};


我知道“世界”为43 23 32 20 25

最佳答案

实际上,Polybius棋盘是2D阵列。要找到Polybius棋盘格元素索引,可以遵循2D数组搜索伪代码

for i=0 : array.length
    for j=0 : array[i].length
        %check the desiare value with array[i][j] element%
    end;
end;


此伪代码在您的代码上的实现:

public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        char[][] cypher = {
            {'p', 'h', '0', 'q', 'g', '6'},
            {'4', 'm', 'e', 'a', '1', 'y'},
            {'l', '2', 'n', 'o', 'f', 'd'},
            {'x', 'k', 'r', '3', 'c', 'v'},
            {'s', '5', 'c', 'w', '7', 'b'},
            {'j', '9', 'u', 't', 'i', '8'}};

        char[] data = {'w', 'o', 'r', 'l', 'd'};//data convert to char array

        for (char c : data) {
            for (int i = 0; i < cypher.length; i++) {
                char[] cs = cypher[i];
                for (int j = 0; j < cs.length; j++) {
                    char d = cs[j];
                    if (d == c) {
                        sb.append(i);
                        sb.append(j);
                        sb.append(" ");
                    }

                }

            }
        }
        System.out.println(sb.toString());
    }


此代码的输出是:43 23 32 20 25

08-17 10:30