如何在 Java 中获取 Polybius 密码中单词的坐标?
How do I get the coordinates of a word in a Polybius cipher in Java?
我正在尝试构建一个 Polybius 密码,我需要对其进行加密和解密。
那么,例如,我最初如何获得这个正方形中单词 "world" 的坐标?
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'},};
我知道 "World" 会是 43 23 32 20 25
实际上 Polybius 棋盘 是一个 2D-Array。要找到 Polybius 棋盘元素索引,您可以按照 2D-Array searching pseudocode
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
我正在尝试构建一个 Polybius 密码,我需要对其进行加密和解密。
那么,例如,我最初如何获得这个正方形中单词 "world" 的坐标?
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'},};
我知道 "World" 会是 43 23 32 20 25
实际上 Polybius 棋盘 是一个 2D-Array。要找到 Polybius 棋盘元素索引,您可以按照 2D-Array searching pseudocode
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