问题描述
我发现了一个可以对字符串进行编码和解码的Java代码,但是它是用较旧的Java编写的,我可以通过什么实现来对其进行更新? 应该使用Queue给我们提供密钥集,并对用户给出的消息进行编码,然后将其解码,所有信息都打印出来。
import jss2.CircularArrayQueue;
public class Codes{
//-----------------------------------------------------------------
// Encode and decode a message using a key of values stored in
// a queue.
//-----------------------------------------------------------------
public static void main ( String[] args)
{
int[] key = {5,12,-3,8,-9,4,10};
Integer keyvalue;
String encoded = "",decoded = "";
String message = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " +
"computers are lousy actors.";
CircularArrayQueue<Integer> keyQueue1 = new CircularArrayQueue<Integer>();
CircularArrayQueue<Integer> keyQueue2 = new CircularArrayQueue<Integer>();
// load key queue
for (int scan=0; scan < key.length; scan++)
{
keyQueue1.enqueue (new Integer(key[scan]));
keyQueue2.enqueue (new Integer(key[scan]));
}
// encode message
for (int scan=0; scan < message.length(); scan++)
{
keyvalue = keyQueue1.dequeue();
encoded += (char) ((int)message.charat(scan) + keyvalue.intValue());
keyQueue1.enqueue (keyvalue);
}
System.out.println ("\n\nEncoded Message:\n\n" + encoded + "\n");
// decode message
for (int scan=0; scan < encoded.length(); scan++)
{
keyvalue = keyQueue2.dequeue();
decoded += (char) ((int)encoded.charat(scan) - keyvalue.intValue());
keyQueue2.enqueue (keyvalue);
}
System.out.println ("Decoded Message:\n\n" + decoded+"\n\n");
}
}
解决方法
您使用keyValue
声明了变量Integer
,但是必须使用int
进行了声明。