在java中解密加密文本
我有个任务。我已经完成了任务的所有部分。但我的任务中有一部分被打断了。你能帮我吗 分配指南 三、 为EncryptedNode类编写一个构造函数方法。此构造函数应将字符串作为参数。 与您编写的其他构造函数不同,此构造函数应该有条件地运行。如果消息长度为一个字符,只需将该字符指定给字母实例变量即可。如果消息长度为两个字符,请将第一个字符指定给字母,将另一个字符作为字符串指定给右侧EncryptedNode。在任何其他情况下,找到输入字符串的中间值,将第一个索引处的字符指定给字母characters 从1到中间的字符应转到右侧EncryptedNode,剩余的未认领 字符应位于左侧EncryptedNode。注意:您将从中实例化新的EncryptedNode对象 此构造函数,从而创建递归构造 四、 编写以下方法: a。decrypt方法不接受任何参数并返回字符串。此方法应通过递归地添加每个EncryptedNode的字母、左和右元素来重新构建原始未加密字符串。这个过程将留给您来解决 我的示例代码是在java中解密加密文本,java,string,encryption,Java,String,Encryption,我有个任务。我已经完成了任务的所有部分。但我的任务中有一部分被打断了。你能帮我吗 分配指南 三、 为EncryptedNode类编写一个构造函数方法。此构造函数应将字符串作为参数。 与您编写的其他构造函数不同,此构造函数应该有条件地运行。如果消息长度为一个字符,只需将该字符指定给字母实例变量即可。如果消息长度为两个字符,请将第一个字符指定给字母,将另一个字符作为字符串指定给右侧EncryptedNode。在任何其他情况下,找到输入字符串的中间值,将第一个索引处的字符指定给字母characters
class EncryptedNode {
public EncryptedNode left, right ;
public char letter ;
// EncryptedNode Class constructor method.
public EncryptedNode (String message) {
// get String length
int message_length = message.length() ;
if (message_length == 1) {
this.letter = message.charAt(0) ;
}else if (message_length == 2) {
this.letter = message.charAt(0) ;
this.right = new EncryptedNode (message.substring(1));
} else {
this.letter = message.charAt(0) ;
// get the middle index of the message string.
int middle_index = message.substring(1).length() /2 ;
// get left and right strings
String rightStr = message.substring(1,middle_index+1);
String leftStr = message.substring(middle_index+1);
this.left = new EncryptedNode (leftStr);
this.right = new EncryptedNode (rightStr);
}
System.out.println (this.letter);
// System.out.println (this.right);
}
public static void main (String [] args) {
EncryptedNode en = new EncryptedNode("ABCDEF") ;
en.decrypt();
}
public String decrypt () {
if (this.left == null && this.right==null) {
return this.letter;
}else if (this.left == null && this.right != null) {
return this.letter + this.right;
}else if (this.left !=null && this.right != null) {
return this.left + this.letter + this.right;
}
}
}我不打算为您编写代码,因为这是一个课堂作业,但我可以解释您应该如何着手解决它 您希望通过加密方法反向工作以对其进行解密。现在,无论加密的字符串是什么,都只返回“st” 我以前误读了密码。 这是一种递归方法,因此您必须从底层开始,然后逐步向上。你必须先解密左边,然后解密右边,然后把它们加在一起 在decrypt方法中,必须递归调用
this.left.decrypt()
和this.right.decrypt()代码>直到到达基本案例。基本情况是,左侧和右侧等于null。在这一点上,你只需回信。这是递归的底部
因此,要编写解密方法,需要返回this.left.decrypt()+this.letter+this.right.decrypt()代码>除非this.left或this.right为空
如果this.right为空,您将返回此.letter代码>
如果this.left为null,您将返回this.letter+this.right.decrypt()代码>
对于最初的困惑,我感到抱歉,我认为这是一个比实际更容易的任务
希望这次我能帮上忙
解决方案:
public String decrypt () {
if (this.right == null) {
return this.letter;
}
if (this.left == null) {
return this.letter + this.right.decrypt();
}
return this.left.decrypt() + this.letter + this.right.decrypt();
}
我不会为你写代码,因为这是一个课堂作业,但我可以解释你应该如何着手解决它
您希望通过加密方法反向工作以对其进行解密。现在,无论加密的字符串是什么,都只返回“st”
我以前误读了密码。
这是一种递归方法,因此您必须从底层开始,然后逐步向上。你必须先解密左边,然后解密右边,然后把它们加在一起
在decrypt方法中,必须递归调用this.left.decrypt()
和this.right.decrypt()代码>直到到达基本案例。基本情况是,左侧和右侧等于null。在这一点上,你只需回信。这是递归的底部
因此,要编写解密方法,需要返回this.left.decrypt()+this.letter+this.right.decrypt()代码>除非this.left或this.right为空
如果this.right为空,您将返回此.letter代码>
如果this.left为null,您将返回this.letter+this.right.decrypt()代码>
对于最初的困惑,我感到抱歉,我认为这是一个比实际更容易的任务
希望这次我能帮上忙
解决方案:
public String decrypt () {
if (this.right == null) {
return this.letter;
}
if (this.left == null) {
return this.letter + this.right.decrypt();
}
return this.left.decrypt() + this.letter + this.right.decrypt();
}
您非常接近一个有效的解决方案。请记住,根据您的规格,您将文本存储为letter+right+left
,而不是(left+letter+right
)
我发布了一个解决方案,仔细看看它是如何工作的。如果在需要更改字符串值时允许使用StringBuilder(或线程安全StringBuffer)(我将在注释中添加StringBuilder连接版本)
您非常接近一个有效的解决方案。请记住,根据您的规格,您将文本存储为letter+right+left
,而不是(left+letter+right
)
我发布了一个解决方案,仔细看看它是如何工作的。如果在需要更改字符串值时允许使用StringBuilder(或线程安全StringBuffer)(我将在注释中添加StringBuilder连接版本)
返回“st”仅用于返回目的。需要解密的消息以字符串形式返回。修复了我的响应,我不知道这是递归的。为您编写。没问题!请选择此作为有效的解决方案。@chinthaka您能选择此答案作为问题的答案,以便我获得声誉吗?返回“st”仅用于返回目的。需要解密的消息以字符串形式返回。修复了我的响应,我不知道这是递归的。为您编写。没问题!如果你的解决方案有效,请选择它作为有效的解决方案。@chinthaka你能选择这个答案作为问题的答案,这样我就可以获得声誉吗?我有错误。需要修复它们。我需要以字符串格式返回解密后的消息。我遇到错误。需要修复它们。我需要返回字符串格式的解密消息