英文:
Initializing a 2D array - cipher table
问题
我需要创建一个密码表,但我不知道该怎么做。这段代码:
public class Prog3Cipher {
// 实例变量
static char[] keyList; // 变量描述注释
static char[][] cipherTable; // 变量描述注释
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public Prog3Cipher(char code, String key) {
String[] keyList = new String[] {"key"};
cipherTable = new char[26][26];
cipherTable[0][0] = 'H';
for (int x = 0; x < cipherTable.length; x++) {
for (int y = 0; y < cipherTable.length; y++) {
cipherTable[x][y] = alpha.charAt(y);
}
}
System.out.println(Arrays.deepToString(cipherTable));
}
}
输出:
[[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], [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], [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],
一遍又一遍地输出。code
和 key
将会被提供,但现在我先使用 'H' 和 'key' 作为输入。表格需要看起来像这样,忽略蓝色的行和列:
(图片在这里,由于文本格式,无法显示图片)
图片中的 code
是 'H',所以 [0][0] 元素是 'H',字母继续在相邻的行和列中。我将使用完成的表格来编码和解码消息,但现在我只需要表格正确。
英文:
I need to create a cipher table and I'm stuck on what to do. This code:
public class Prog3Cipher {
// INSTANCE VARIABLES
static char [ ] keyList; // VARIABLE DESCRIPTION COMMENT
static char [ ][ ] cipherTable; // VARIABLE DESCRIPTION COMMENT
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public Prog3Cipher( char code, String key ) {
String[] keyList = new String []{"key"};
cipherTable = new char[26][26];
cipherTable[0][0] = 'H';
for(int x = 0; x < cipherTable.length; x++){
for(int y = 0; y < cipherTable.length; y++){
cipherTable[x][y] = alpha.charAt(y);
}
}
System.out.println(Arrays.deepToString(cipherTable));
}
outputs:
[[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], [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], [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],
over and over. the code
and key
will be given in but for now i have 'H' and 'key' as inputs. the table needs to look like this, ignoring the blue row and column:
The code
in the pic is 'H', so the [0][0] element is 'H' and the alphabet continues in the adjacent row and column. I will use the completed table to encode and decode messages but for now I just need the table to be correct.
答案1
得分: 5
基于你分享的图片,你可以这样说,对于cipherTable
中的每个单元格,字符应该是行索引 + 列索引 + 7(一个任意的魔术数字,看起来是这样)位置上的字符,当然要对字母表的大小取模。如果我们用 Java 表达这个过程:
int offset = 'H' - 'A';
cipherTable = new char[26][26];
for (int x = 0; x < cipherTable.length; x++) {
for(int y = 0; y < cipherTable[0].length; y++) {
cipherTable[x][y] = alpha.charAt((x + y + offset) % alpha.size());
}
}
英文:
Based on the image you shared, you could say that for each cell in the cipherTable
, the character should be the character in the position of the row index + the column index + 7 (an arbitrary magic number, seemingly), modulo the size of the alphabet, of course. If we express this is Java:
int offset = 'H' - 'A';
cipherTable = new char[26][26];
for (int x = 0; x < cipherTable.length; x++) {
for(int y = 0; y < cipherTable[0].length; y++) {
cipherTable[x][y] = alpha.charAt((x + y + offset) % alpha.size());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论