英文:
Caesar Cipher after the first space in a given String starts adding a char ( 'C' ) to the beginning of every word after that space
问题
我的凯撒密码在消息的每个单词之后的第一个单词之前添加了一个"C"。所以我不确定是否可能与我如何处理给定消息中的空格有关?
期望输出:
你的消息?
进攻黎明的虫群
编码密钥?
3
DWWDFN CHUJ DW GDZQ
当前输出:
你的消息?
进攻黎明的虫群
编码密钥?
3
DWWDFN CCHUJ CDW CGDZQ
public static void rot() {
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};
String rotM = "";
Scanner scnr = new Scanner(System.in);
System.out.print("你的消息? ");
String message = scnr.nextLine();
System.out.print("编码密钥? ");
int key = scnr.nextInt();
message = message.toLowerCase();
for (int i = 0; i < message.length(); i++) {
if (!Character.isLetter(message.charAt(i))) rotM += " ";
int origNumIndex = new String(alphabet).indexOf(message.charAt(i));
int newIndex = origNumIndex + key;
if (newIndex > 25) newIndex = newIndex - 26;
char newLetter = alphabet[newIndex];
rotM += newLetter;
}
System.out.println(rotM.toUpperCase());
}
英文:
My Caesar cipher is adding a C at the beginning of every word after the first word in the message I want to rot. So I'm not sure if maybe it has to do with how I'm handling the spaces in the given message?
Expected output:
Your message?
Attack zerg at dawn
Encoding key?
3
DWWDFN CHUJ DW GDZQ
Current output:
Your message?
Attack zerg at dawn
Encoding key?
3
DWWDFN CCHUJ CDW CGDZQ
public static void rot() {
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};
String rotM = "";
Scanner scnr = new Scanner(System.in);
System.out.print("Your message? ");
String message = scnr.nextLine();
System.out.print("Encoding key? ");
int key = scnr.nextInt();
message = message.toLowerCase();
for (int i = 0; i < message.length(); i++) {
if (!Character.isLetter(message.charAt(i))) rotM += " ";
int origNumIndex = new String(alphabet).indexOf(message.charAt(i));
int newIndex = origNumIndex + key;
if (newIndex > 25) newIndex = newIndex - 26;
char newLetter = alphabet[newIndex];
rotM += newLetter;
}
System.out.println(rotM.toUpperCase());
}
答案1
得分: 1
你忘记了一个 continue
语句。没有这个 continue
,它会尝试对一个未知的索引进行编码。我添加了一个容错机制,并对你的代码进行了一些修复和改进:
public static void rot() {
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
String alphabetString = new String(alphabet);
StringBuilder rotM = new StringBuilder();
Scanner scnr = new Scanner(System.in);
System.out.print("你的消息? ");
String message = scnr.nextLine();
System.out.print("编码密钥? ");
int key = scnr.nextInt();
message = message.toLowerCase();
for (int i = 0; i < message.length(); i++) {
char ch = message.charAt(i);
if (!Character.isLetter(ch)) {
rotM.append(" ");
continue; // <- 这个语句之前缺失
}
int index = alphabetString.indexOf(ch);
if (index == -1)
throw new IllegalStateException("非法字符 '" + ch + "'");
rotM.append(alphabet[(index + key) % alphabet.length]);
}
System.out.println(rotM.toString().toUpperCase());
}
英文:
You forget a continue
-statement. Without this continue
, it tries to encode an unknown index. I added a failsafe, fixed and improved your code a bit:
public static void rot() {
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
String alphabetString = new String(alphabet);
StringBuilder rotM = new StringBuilder();
Scanner scnr = new Scanner(System.in);
System.out.print("Your message? ");
String message = scnr.nextLine();
System.out.print("Encoding key? ");
int key = scnr.nextInt();
message = message.toLowerCase();
for (int i = 0; i < message.length(); i++) {
char ch = message.charAt(i);
if (!Character.isLetter(ch)) {
rotM.append(" ");
continue; // <- This statement was missing
}
int index = alphabetString.indexOf(ch);
if (index == -1)
throw new IllegalStateException("Illegal character '" + ch + "'");
rotM.append(alphabet[(index + key) % alphabet.length]);
}
System.out.println(rotM.toString().toUpperCase());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论