Java凯撒密码程序使用sc.nextLine()无法输入含空格字符串的问题
凯撒密码程序输入问题解决
问题原因
使用sc.nextInt()读取旋转位数后,输入缓冲区中会残留一个换行符。后续调用sc.nextLine()时,会直接读取这个换行符,得到空字符串,导致加密/解密的内容为空。
解决方案
有两种可行的修复方式:
方式一:消耗遗留的换行符
在int r = sc.nextInt();之后,添加一行sc.nextLine();,专门读取并丢弃输入缓冲区里的换行符,这样后续的sc.nextLine()就能正常读取用户输入的消息了。
方式二:统一用nextLine()读取输入
先通过sc.nextLine()读取包含数字的整行内容,再将其转换为整数,从根源上避免换行符残留问题。
修改后的代码示例
方式一的修改版
import java.util.*; public class CaesarCipher { protected char[] encoder = new char[26]; protected char[] decoder = new char[26]; /** Constructor that initializes the encryption and decryption arrays */ public CaesarCipher(int rotation) { for(int k=0;k<26;k++) { encoder[k] = (char)('A' + (k + rotation)%26); decoder[k] = (char)('A' + (k - rotation+26)%26); } } /** Returns String representing encrypted message. */ public String encrypt(String message) { return transform(message,encoder); } public String decrypt(String Secret) { return transform(Secret, decoder); } public String transform(String Original,char[] code) { char msg[] = Original.toCharArray(); for(int k=0;k<msg.length;k++) { if(Character.isUpperCase(msg[k])) { int j = msg[k] - 'A'; msg[k] = code[j]; } } return new String(msg); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter any number between 1 and 26 : "); int r = sc.nextInt(); sc.nextLine(); // 新增:消耗遗留的换行符 System.out.println(); CaesarCipher cipher = new CaesarCipher(r); System.out.println("Encryption code : " + new String(cipher.encoder)); System.out.println("Decryption code : " + new String(cipher.decoder)); System.out.println(); System.out.println("Enter a Message to Encrypt : "); String message = sc.nextLine(); String coded = cipher.encrypt(message.toUpperCase()); System.out.println("Encrypted Message : " + coded); System.out.println(); System.out.println("Decrypted Message : " + cipher.decrypt(coded)); sc.close(); } }
方式二的修改版
import java.util.*; public class CaesarCipher { protected char[] encoder = new char[26]; protected char[] decoder = new char[26]; /** Constructor that initializes the encryption and decryption arrays */ public CaesarCipher(int rotation) { for(int k=0;k<26;k++) { encoder[k] = (char)('A' + (k + rotation)%26); decoder[k] = (char)('A' + (k - rotation+26)%26); } } /** Returns String representing encrypted message. */ public String encrypt(String message) { return transform(message,encoder); } public String decrypt(String Secret) { return transform(Secret, decoder); } public String transform(String Original,char[] code) { char msg[] = Original.toCharArray(); for(int k=0;k<msg.length;k++) { if(Character.isUpperCase(msg[k])) { int j = msg[k] - 'A'; msg[k] = code[j]; } } return new String(msg); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter any number between 1 and 26 : "); // 先读取整行再转整数 int r = Integer.parseInt(sc.nextLine()); System.out.println(); CaesarCipher cipher = new CaesarCipher(r); System.out.println("Encryption code : " + new String(cipher.encoder)); System.out.println("Decryption code : " + new String(cipher.decoder)); System.out.println(); System.out.println("Enter a Message to Encrypt : "); String message = sc.nextLine(); String coded = cipher.encrypt(message.toUpperCase()); System.out.println("Encrypted Message : " + coded); System.out.println(); System.out.println("Decrypted Message : " + cipher.decrypt(coded)); sc.close(); } }
内容的提问来源于stack exchange,提问作者Mikhil Angre
相关产品推荐
相关产品推荐

