如何实现无输入消息打印及每行一问的用户输入交互?
修复Java密码程序中交互提示挤行的问题
需求:程序需实现两项功能:1. 打印无需用户输入的提示消息(如欢迎语);2. 每行输出一个交互问题,等待用户输入完成后再显示下一个问题。
现象:当前运行时多个提示语句挤在同一行,例如:Do you want to [E]ncrypt or [D]ecrypt? Do you want to use a [S]hift cipher or [V]igenere Cipher? Do you want to continue(Y/N)?
问题原因
程序开头多余的keyboard.nextLine()读取了欢迎语后的空行,导致第一次进入循环时,第一个operation = keyboard.nextLine()直接获取到空字符串,程序不会等待用户输入就执行下一个提示语句,最终造成多个提示挤在一行。
修复后的完整代码
public static void main(String[] args) { Cipher cipher; // 创建Cipher类型变量 // 打印欢迎消息 Scanner keyboard = new Scanner(System.in); // 创建Scanner对象用于用户输入 System.out.println("Welcome to Nicholas Coleman's Cipher Program!\n"); /* 将所有逻辑放入while循环,支持用户多次重复操作 */ while (true) { /* 提示用户选择加密或解密,再选择密码类型 将输入分别存入operation和whichCipher变量 */ System.out.print("Do you want to [E]ncrypt or [D]ecrypt? "); String operation = keyboard.nextLine(); System.out.print("Do you want to use a [S]hift cipher or [V]igenere Cipher? "); String whichCipher = keyboard.nextLine(); /* 判断要执行的操作 */ if(operation.equals("E")) { // 用户选择加密 if(whichCipher.equals("S")) { // 用户选择ShiftCipher /* 提示输入密钥,创建ShiftCipher实例,调用encrypt方法加密明文 */ System.out.print("Please enter a number between 0 and 25 to use as a key: "); int key = keyboard.nextInt(); keyboard.nextLine(); // 读取nextInt()留下的换行符 System.out.print("Please enter the plaintext to be encrypted: "); String plaintext = keyboard.nextLine(); cipher = new ShiftCipher(key); System.out.println("The corresponding ciphertext is: " + cipher.encrypt(plaintext)); } else if(whichCipher.equals("V")) { // 用户选择VigenereCipher /* 提示输入关键词,创建VigenereCipher实例,调用encrypt方法加密明文 */ System.out.print("Please enter a keyword: "); String keyword = keyboard.nextLine(); System.out.print("Please enter the plaintext to be encrypted: "); String plaintext = keyboard.nextLine(); cipher = new VigenereCipher(keyword); System.out.println("The corresponding ciphertext is: " + cipher.encrypt(plaintext)); } } else if (operation.equals("D")) { // 用户选择解密 if(whichCipher.equals("S")) { // 用户选择ShiftCipher /* 提示输入密钥,创建ShiftCipher实例,调用decrypt方法解密密文 */ System.out.print("Please enter a number between 0 and 25 to use as a key: "); int key = keyboard.nextInt(); keyboard.nextLine(); // 读取nextInt()留下的换行符 System.out.print("Please enter the ciphertext to be decrypted: "); String ciphertext = keyboard.nextLine(); cipher = new ShiftCipher(key); System.out.println("The corresponding plaintext is: " + cipher.decrypt(ciphertext)); } else if(whichCipher.equals("V")) { // 用户选择VigenereCipher /* 提示输入关键词,创建VigenereCipher实例,调用decrypt方法解密密文 */ System.out.print("Please enter a keyword: "); String keyword = keyboard.nextLine(); System.out.print("Please enter the ciphertext to be decrypted: "); String ciphertext = keyboard.nextLine(); cipher = new VigenereCipher(keyword); System.out.println("The corresponding plaintext is: " + cipher.decrypt(ciphertext)); } } // 询问用户是否继续 System.out.print("Do you want to continue(Y/N)? "); String cont = keyboard.nextLine(); // 如果用户输入Y,继续循环 if(cont.equals("Y")) { continue; } // 如果用户输入N,显示感谢信息并终止循环 else if(cont.equals("N")) { System.out.println("Thank you for using Nicholas Coleman's cipher program."); break; } } }
关键修改说明
- 删除了程序开头多余的
String nextLine = keyboard.nextLine();,避免提前读取空行导致后续输入步骤被跳过。 - 修正了Vigenere解密部分的两处语义错误:
- 将提示文本中的
plaintext to be decrypted改为ciphertext to be decrypted,符合解密操作的输入逻辑。 - 将输出结果中的
corresponding ciphertext改为corresponding plaintext,匹配解密操作的输出结果。
- 将提示文本中的
- 保留了
nextInt()后的keyboard.nextLine(),用于清除输入缓冲区中的换行符,避免后续nextLine()读取到空字符串。
内容的提问来源于stack exchange,提问作者Pooh_86
相关产品推荐
相关产品推荐

