1 Star 0 Fork 0

CS-IMIS-23/20172306

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Codes.java 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
20172306 提交于 7年前 . 书上代码
package wek3;
import java.util.*;
/**
* Codes demonstrates the use of queues to encrypt and decrypt messages.
*
* @author Lewis and Chase
* @version 4.0
*/
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 = "All programmers are playwrights and all " +
"computers are lousy actors.";
Queue<Integer> encodingQueue = new LinkedList<Integer>();
Queue<Integer> decodingQueue = new LinkedList<Integer>();
// load key queues
for (int scan = 0; scan < key.length; scan++)
{
encodingQueue.add(key[scan]);
decodingQueue.add(key[scan]);
}
// encode message
for (int scan = 0; scan < message.length(); scan++)
{
keyValue = encodingQueue.remove();
encoded += (char) (message.charAt(scan) + keyValue);
encodingQueue.add(keyValue);
}
System.out.println ("Encoded Message:\n" + encoded + "\n");
// decode message
for (int scan = 0; scan < encoded.length(); scan++)
{
keyValue = decodingQueue.remove();
decoded += (char) (encoded.charAt(scan) - keyValue);
decodingQueue.add(keyValue);
}
System.out.println ("Decoded Message:\n" + decoded);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172306.git
git@gitee.com:CS-IMIS-23/20172306.git
CS-IMIS-23
20172306
20172306
master

搜索帮助