gpt4 book ai didi

java - 需要帮助在 java 中实现 "matrix rotation"密码

转载 作者:行者123 更新时间:2023-12-01 10:30:46 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的密码程序,或多或少将 ASCII 文本转换为二进制矩阵,因此“AAAA”将变为:

01000001  
01000001
01000001
01000001

然后旋转它以使得:

1111  
0000
0000
0000
0000
0000
1111
0000

我面临的问题是,似乎只要有一个“0”就不会返回任何内容,所以我最终得到的只是:

1111  
1111

这是我的代码:

public class Cipher {
public static void main(String[] args) {
Scanner i = new Scanner(System.in);
System.out.print("1. Cipher text\n2. Decipher Text\n>");
int choice = i.nextInt();
if (choice == 1) {
System.out.println("Enter text to be ciphered: ");
String text = i.next();
String[] a = new String[text.length()];
String[] b = new String[text.length()];
String[] c = new String[text.length()];
String[] d = new String[text.length()];
String[] e = new String[text.length()];
String[] f = new String[text.length()];
String[] g = new String[text.length()];
String aa = "0";
String bb = "0";
String cc = "0";
String dd = "0";
String ee = "0";
String ff = "0";
String gg = "0";
for (int n = 0; n < 7; n++) {
for (int k = 6; k >= 0; k--) {
for (int j = 0; j < text.length(); j++) {
String buffer = Integer.toBinaryString(text.charAt(j));
switch (n) {
case 0: a[j] = buffer.substring(k, k + 1); break;
case 1: b[j] = buffer.substring(k, k + 1); break;
case 2: c[j] = buffer.substring(k, k + 1); break;
case 3: d[j] = buffer.substring(k, k + 1); break;
case 4: e[j] = buffer.substring(k, k + 1); break;
case 5: f[j] = buffer.substring(k, k + 1); break;
case 6: g[j] = buffer.substring(k, k + 1); break;
default: break;
}
}
}
}
for (int n = 0; n < 7; n++) {
for (int m = 0; m < text.length(); m++) {
System.out.println(a[m]);
}
}
} else if (choice == 2) {
}
}
}

以下是我希望它如何工作的示例:

输入的字符串:

ABCD

第一步:

01000001  
01000010
01000011
01000100

“逆时针旋转”:

1010  
0110
0001
0000
0000
0000
1111
0000

编辑:

这个过程是这样的:

  1. 一串 ASCII 字符转换为等效的二进制 ASCII

    A - 01000001
    B - 01000010
    C - 01000011
    D - 01000100
    E - 01000101
    F - 01000110
    G - 01000111

    等等

在这个例子中,我将使用字符串“ABABAC”,它会变成:

A -> 01000001
B -> 01000010
A -> 01000001
B -> 01000010
A -> 01000001
C -> 01000010
  • 矩阵由字符串组成。字符串“ABABAC”变成矩阵:

    01000001
    01000010
    01000001
    01000010
    01000001
    01000100
  • 注意:在我的代码中,这是以下部分:

    String buffer = Integer.toBinaryString(text.charAt(j));

    text 是用户输入的原始字符串,对于第一个循环,j 将为 0 或字符串文本中的第一个字符,即 A,它将定义缓冲区为 01000001。对于第二个循环,字符串的第二个字符text,如果是B,则将缓冲区定义为01000010。此过程循环进行,直到整个字符串文本已转换为二进制。

  • 这个矩阵逆时针旋转90度变成

    101010
    010100
    000001
    000000
    000000
    000000
    111111
    000000
  • 在我的代码中,这部分是:

                        switch (n) {
    case 0: a[j] = buffer.substring(k, k + 1); break;
    case 1: b[j] = buffer.substring(k, k + 1); break;
    case 2: c[j] = buffer.substring(k, k + 1); break;
    case 3: d[j] = buffer.substring(k, k + 1); break;
    case 4: e[j] = buffer.substring(k, k + 1); break;
    case 5: f[j] = buffer.substring(k, k + 1); break;
    case 6: g[j] = buffer.substring(k, k + 1); break;
    default: break;

    这里,对于第一次迭代,情况是 n 为 0,j 为 0,因此选择第一种情况,并将数组 a[] 的 0 位置设置为 1,因为“01000001”的最后一个字符是 1 . 对于第二次迭代,处理字符 B。 B 是“01000010”,因此当读取最后一个字符时,返回 0。 N 为 0,因此选择情况 0,但 j 为 1,因此数组 A 的第二个位置设置为“0”,因为“01000010”的最后一个字符是“0”。

    换句话说,每个数组的最后一个字符将成为第一个数组,每个数组的倒数第二个字符将成为第二个字符串,依此类推。

    本质上,我正在做的过程是要求用户提供一个字符串,将字符串转换为等效的 ASCII 字符串,然后运行一些嵌套的 FOR 循环以从最后一个、倒数第二个、倒数第三个等生成数组。每个字符串的字符。我面临的问题是,每个 0 似乎都作为 null 发送,而不是作为实际的 0 发送到数组,导致输出全部为“1”。

    最佳答案

    我如何可视化穿过数组的二维循环 Matrix sorting

    密码类

    要创建密码,只需按照 CipherhiddenText = new Cipher(userInput); 的方式进行操作

    /**
    * For the thread: http://stackoverflow.com/questions/35110522/need-help-implementing-matrix-rotation-in-java-for-cipher
    *
    * @author Riley C
    * @version 1/30/16
    */
    public class Cipher
    {
    // instance variables
    private String text;
    private String[] matrix;
    private String[] encodedMatrix;

    /**
    * Constructor for objects of class Cipher
    */
    public Cipher(String text)
    {
    this.text = text;
    matrix = new String[text.length()];
    for (int i = 0; i < matrix.length; i++) // Sets each point of the matrix array to binary string
    {
    matrix[i] = "0" + Integer.toBinaryString(text.charAt(i));
    System.out.println(matrix[i]);
    }

    encodedMatrix = new String[8]; // Needs to hold 8 slots
    for (int i = 0; i < 8; i++) // Will reverse the matrix counter clockwise...
    {
    encodedMatrix[i] = "";
    for (int j = 0; j < matrix.length; j++)
    encodedMatrix[i] += matrix[j].charAt(7 - i);
    System.out.println(encodedMatrix[i]);
    }
    }

    // getters and setters
    public String getText() {return text;}
    public String[] getMatrix() // Making dat deep copy
    {
    String[] returnMatrix = new String[this.matrix.length];
    for (int i = 0; i < returnMatrix.length; i++)
    returnMatrix[i] = this.matrix[i];
    return returnMatrix;
    }
    public String[] getEncodedMatrix() // ...
    {
    String[] returnMatrix = new String[this.encodedMatrix.length];
    for (int i = 0; i < returnMatrix.length; i++)
    returnMatrix[i] = this.encodedMatrix[i];
    return returnMatrix;
    }
    }

    运行类

    /**
    * Used to test the cipher class
    */
    public class CipherTest
    {
    public static void main(String[] args)
    {
    Cipher hello = new Cipher("hello");
    System.out.println(hello.getText() + hello.getMatrix() + hello.getEncodedMatrix());
    }
    }

    输出

    01101000
    01100101
    01101100
    01101100
    01101111
    01001
    00001
    01111
    10111
    00000
    11111
    11111
    00000
    hello[Ljava.lang.String;@16dfc0a[Ljava.lang.String;@1bafa2b

    数学中的矩阵与计算机科学中的数组请注意,它是 array[1]

    之后的空格

    matrix vs array

    关于java - 需要帮助在 java 中实现 "matrix rotation"密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110522/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com