[英]How do I get the coordinates of a word in a Polybius cipher in Java?
I'm trying to build a Polybius cipher which I will need to encrypt and decrypt. 我正在尝试构建需要加密和解密的Polybius密码。
So for instance, how do I initially get the coordinates for the word "world" in this square? 因此,例如,我最初如何获得该正方形中“世界”一词的坐标?
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'},};
I know "World" would be 43 23 32 20 25 我知道“世界”为43 23 32 20 25
Actually Polybius checkerboard is a 2D-Array . 实际上, Polybius棋盘是2D-Array 。 to finding a Polybius checkerboard element index you can follow 2D-Array searching pseudocode
要找到Polybius棋盘格元素索引,可以遵循2D数组搜索伪代码
for i=0 : array.length
for j=0 : array[i].length
%check the desiare value with array[i][j] element%
end;
end;
implementation of this pseudocode on your code: 此伪代码在您的代码上的实现:
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());
}
output of this code is : 43 23 32 20 25
此代码的输出是:
43 23 32 20 25
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.