java - 如何在密码程序中包含空格?
问题描述
该代码询问您是否要对消息进行编码或解码,然后它会询问该消息。它将通过以下参考工作:“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./<>?;:'”[]{}=+-_()*&^%$#@!~`0123456789"
"kngcadsxbvfhjtiumylzqropweKNGCADSXBVFHJTIUMYLZQROPWE,./<>?;:'"[]{}=+-_()*&^%$#@!~`0123456789"
因此,例如,如果您尝试对字母“a”进行编码,它将输出字母“k”。
我的问题是我在输入消息时不能包含任何空格。
这是我的代码:
import java.util.Scanner;
public class SecretMessage {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter 1 to encode, 2 to decode, 3 to quit:");
int start = input.nextInt();
if (start == 3){
break;
}
System.out.println("Type your message:");
String test = input.next();
String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./<>?;:’”[]{}=+-_()*&^%$#@!~`0123456789 ";
String enc = "kngcadsxbvfhjtiumylzqropweKNGCADSXBVFHJTIUMYLZQROPWE,./<>?;:’”[]{}=+-_()*&^%$#@!~`0123456789 ";
char[] array = test.toCharArray();
char[] decoded = letters.toCharArray();
char[] encoded = enc.toCharArray();
int[] position = new int[array.length];
char[] end = new char[array.length];
if (start == 1){
for (int i = 0; i < test.length(); i++){
for (int j = 0; j < decoded.length; j++){
if (array[i] == decoded[j]){
position[i] = j;
}
}
}
for (int f = 0; f < test.length(); f++){
end[f] = encoded[position[f]];
}
for (int x = 0; x < test.length(); x++){
System.out.print(end[x]);
}
System.out.println(" ");
} else {
for (int i = 0; i < test.length(); i++){
for (int j = 0; j < encoded.length; j++){
if (array[i] == encoded[j]){
position[i] = j;
}
}
}
for (int f = 0; f < test.length(); f++){
end[f] = decoded[position[f]];
}
String output = new String(end);
System.out.println(output);
}
System.out.println(" ");
} while (1 ==1);
}
}
解决方案
推荐阅读
- python - Lambda 函数 - 一个有趣的问题
- node.js - 在 Git/VS Code 中创建新分支时,CSS 文件更改不断重置
- sql-server - 需要帮助调整特定输出的查询
- r - 将闪亮的 DT 单元格复制到用户剪贴板
- angular - 反应式表单异步验证器未将错误设置为 FormGroup
- python - 如何使用 sklearn 转换器创建两组特征之间的所有交互变量?
- saml-2.0 - 如果未在服务集合中配置 .pfx 文件,则 Sustainsys.Saml2 注销不起作用
- c++ - 有没有一种巧妙的方法可以避免在 C++ 中使用嵌套类进行额外填充?
- firebase - 添加 firebase 插件会影响“flutter run”的行为方式吗?
- python - 基于掩码删除日期子字符串