如何使用二维数组加密和解密字符串?

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

How to encrypt and decrypt the string using 2d array?

问题

以下是翻译好的内容:

加密代码:

import java.util.Scanner;

public class Secret {

    public static void main(String[] args) {
        Scanner x = new Scanner(System.in);

        System.out.println("输入需要加密的句子:");
        String y = x.nextLine();

        int len = y.length();

        System.out.println("句子长度为:" + len);

        System.out.print("输入列数:");
        int arr = x.nextInt();

        char[][] z = new char[6][arr];

        // 将字符填充到数组中 //
        int pos = 0;
        for (int i = 0; i < z.length; i++) {
            for (int j = 0; j < z[i].length; j++) {
                z[i][j] = y.charAt(pos);
                pos++;
            }
        }
        
        // 按列从上到下输出加密文本 //
        for (int j = 0; j < z[arr].length; j++) {
            System.out.print(z[0][j] + "" + z[1][j] + "" + z[2][j] + "" + z[3][j] + "" + z[4][j]
                    + "" + z[5][j]);
        }
    }
}

解密代码:

import java.util.Scanner;

public class Secret1 {

    public static void main(String[] args) {

        Scanner x = new Scanner(System.in);

        System.out.println("输入需要解密的句子:");
        String str = x.nextLine();

        int len = str.length();

        System.out.println("句子长度为:" + len);

        System.out.println("输入行数:");
        int arr = x.nextInt();

        char[][] z = new char[arr][6];

        // 将字符填充到数组中 //
        int pos = 0;
        for (int i = 0; i < z.length; i++) {
            for (int j = 0; j < z[i].length; j++) {
                z[i][j] = str.charAt(pos);
                pos++;
            }
        }

        // 按行从左到右输出解密文本 //
        for (int j = 0; j < 6; j++) {
            for (int i = 0; i < z.length; i++) {
                System.out.print(z[i][j]);
            }
        }
    }
}

请注意,由于你提供的代码中包含了 HTML 编码(如 &quot; 代表双引号),因此在翻译时这些编码也会得到保留。如果你需要实际的双引号而不是编码,请在代码中进行适当的更改。至于你的问题,涉及到的功能似乎已经被翻译并包含在了上面的代码中。

英文:

I actually manage to encrypt and decrypt the plain string using 2d array.

Below is my encrypt code:

import java.util.Scanner;
public class Secret {
public static void  main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println(&quot;Enter a sentence that need to be encrypt : &quot;);
String y = x.nextLine();
int len = y.length();
System.out.println(&quot;The length of the sentences is : &quot; + len);
System.out.print(&quot;Enter number of column : &quot; );
int arr = x.nextInt();	
char [] [] z = new char [6] [arr];
// fill the character into array //
int pos = 0;
for (int i = 0; i&lt;z.length; i++) {
for (int j = 0; j&lt;z[i].length; j++) {
z[i][j] = y.charAt(pos);
pos++;
}
}
// output the encrypted text by reading the array downward, column by
column.//
for (int j = 0; j&lt;z[arr].length; j++) {
System.out.print(z[0][j] + &quot;&quot; + z[1][j] + &quot;&quot; + z[2][j] + &quot;&quot; + z[3][j] + &quot;&quot; + z[4][j] 
+ &quot;&quot; + z[5][j]);}	
}
}

Input:

Enter a sentence that need to be encrypt : 
Hello World.
The length of the sentences is : 12
Enter number of column : 2

Output:

HloWrdel ol. 

But my desired output is something like this HloWrdel*ol... which means that the space is replace by &quot;*&quot; and if have extra empty array print &quot;.&quot; instead.

Right now, my array size is depend on string length so when I input arr = 3 ( 6 X 3 = 18 ) it show the error like this

Enter a sentence that need to be encrypt : 
Hello World.
The length of the sentences is : 12
Enter number of column : 3
Exception in thread &quot;main&quot; java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Secret.main(Secret.java:33)

Therefore, my question is how to come out the desired output like this HloWrdel*ol...? The space becomes &quot;*&quot; and there is no limit for entering the array size. When I enter the array size (column) that is more than the string length, the character of the string automatically fill up the array and the extra empty array will print &quot;.&quot; instead.

For decryption code:

    import java.util.Scanner;
public class Secret1 {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println(&quot;Enter the sentences that need to decrypt : &quot;);
String str = x.nextLine();
int len = str.length();
System.out.println(&quot;The number of length is &quot; + len);
System.out.println(&quot;Enter the number of rows : &quot; );
int arr = x.nextInt();
char [][] z = new char [arr][6];
// fill character into array //
int pos = 0;
for(int i = 0; i&lt;z.length; i++) {
for(int j = 0; j&lt;z[i].length; j++) {
z[i][j] = str.charAt(pos);
pos++ ;
}
}
//output the decrypted text by reading the array from left to right, row by
row.//
for (int i = 0; i&lt;z.length; i++) {
System.out.print(z[i][0]);
}
for (int i = 0; i&lt;z.length; i++) {
System.out.print(z[i][1]);
}
for (int i = 0; i&lt;z.length; i++) {
System.out.print(z[i][2]);
}
for (int i = 0; i&lt;z.length; i++) {
System.out.print(z[i][3]);
}
for (int i = 0; i&lt;z.length; i++) {
System.out.print(z[i][4]);
}
for (int i = 0; i&lt;z.length; i++) {
System.out.print(z[i][5]);
}
}
}

Input:

Enter the sentences that need to decrypt : 
HloWrdel ol.
The number of length is 12
Enter the number of rows : 
2

Output:

Hello World.

But my desired output is from encrypted text HloWrdel*ol... into plain text Hello World.. How can I get such output?

Besides, the decryption code is also depend on string length so when I enter the array size more than string length, it show like this:

Enter the sentences that need to decrypt : 
HloWrdel ol.
The number of length is 12
Enter the number of rows : 
3
Exception in thread &quot;main&quot; java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Secret1.main(Secret1.java:147)

Is it possible the array size that I have enter is not limited to the string length but print nothing for the extra empty array after filling into the array?

答案1

得分: -1

在你的最内层循环(for (int j = 0; j&lt;z[i].length; j++))中,检查是否 pos &gt;= len,如果是这样,跳过尝试读取并用 '.' 填充二维元素的步骤。

if (pos&gt;=len)
{
    z[i][j]=&#39;.&#39;;
    continue;
}

// 你的正常代码

然后在填充元素后,检查你是否刚刚用空格填充了它。

if (z[i][j]==&#39; &#39;)
    z[i][j]=&#39;*&#39;;
英文:

Inside your innermost loop (the for (int j = 0; j&lt;z[i].length; j++)), check if pos &gt;= len, and if so, skip attempting to read and fill the 2D element with '.'

if (pos&gt;=len)
{
z[i][j]=&#39;.&#39;;
continue;
}
//your normal code

And then after you fill the element, check if you've just filled it with space

if (z[i][j]==&#39; &#39;)
z[i][j]=&#39;*&#39;;

huangapple
  • 本文由 发表于 2020年10月18日 13:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64410062.html
匿名

发表评论

匿名网友

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

确定