英文:
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 编码(如 "
代表双引号),因此在翻译时这些编码也会得到保留。如果你需要实际的双引号而不是编码,请在代码中进行适当的更改。至于你的问题,涉及到的功能似乎已经被翻译并包含在了上面的代码中。
英文:
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("Enter a sentence that need to be encrypt : ");
String y = x.nextLine();
int len = y.length();
System.out.println("The length of the sentences is : " + len);
System.out.print("Enter number of column : " );
int arr = x.nextInt();
char [] [] z = new char [6] [arr];
// fill the character into array //
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++;
}
}
// output the encrypted text by reading the array downward, column by
column.//
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]);}
}
}
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 "*"
and if have extra empty array print "."
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 "main" 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 "*"
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 "."
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("Enter the sentences that need to decrypt : ");
String str = x.nextLine();
int len = str.length();
System.out.println("The number of length is " + len);
System.out.println("Enter the number of rows : " );
int arr = x.nextInt();
char [][] z = new char [arr][6];
// fill character into array //
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++ ;
}
}
//output the decrypted text by reading the array from left to right, row by
row.//
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][0]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][1]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][2]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][3]);
}
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][4]);
}
for (int i = 0; i<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 "main" 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<z[i].length; j++)
)中,检查是否 pos >= len
,如果是这样,跳过尝试读取并用 '.' 填充二维元素的步骤。
if (pos>=len)
{
z[i][j]='.';
continue;
}
// 你的正常代码
然后在填充元素后,检查你是否刚刚用空格填充了它。
if (z[i][j]==' ')
z[i][j]='*';
英文:
Inside your innermost loop (the for (int j = 0; j<z[i].length; j++)
), check if pos >= len
, and if so, skip attempting to read and fill the 2D element with '.'
if (pos>=len)
{
z[i][j]='.';
continue;
}
//your normal code
And then after you fill the element, check if you've just filled it with space
if (z[i][j]==' ')
z[i][j]='*';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论