初始化一个二维数组 – 密码表

huangapple go评论69阅读模式
英文:

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],

一遍又一遍地输出。codekey 将会被提供,但现在我先使用 '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 = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;

    public Prog3Cipher( char code, String key ) {
        String[] keyList = new String []{&quot;key&quot;};
        cipherTable = new char[26][26];
        cipherTable[0][0] = &#39;H&#39;;
        for(int x = 0; x &lt; cipherTable.length; x++){
            for(int y = 0; y &lt; 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 = &#39;H&#39; - &#39;A&#39;;
cipherTable = new char[26][26];
for (int x = 0; x &lt; cipherTable.length; x++) {
    for(int y = 0; y &lt; cipherTable[0].length; y++) {
        cipherTable[x][y] = alpha.charAt((x + y + offset) % alpha.size());
    }
}

huangapple
  • 本文由 发表于 2020年9月28日 04:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64092772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定