Java Pig Latin翻译器循环异常:重复输出首次输入无法获取新输入
猪拉丁语翻译器循环输入问题解决
问题描述
我决定不借助YouTube教程,从零开始编写首个应用程序,目前对成果还算满意,但程序仅重复输出用户首次输入的内容/翻译结果,无法请求新输入。我已将几乎所有代码放入while循环,期望程序能重新运行并获取新输入,尝试过使用布尔/字符串参数但仍未解决问题,相关代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Have fun using my Pig Latin translator!"); System.out.println("Those are the rules:"); System.out.println("Words that start with a vowel (A, E, I, O, U) simply have \"ay\" appended to the end of the word. "); System.out.println("Words that start with a consonant have all consonant letters up to the first vowel moved to the end of the word, and \"-ay\" is appended. -('Y' is counted as a vowel in this context). "); System.out.println("Write a sentence or word to translate into Pig Latin!"); String userInput = scanner.nextLine(); // Loop to translate another word/sentence while (true) { // User input, checking is word/sentence contains only letters or " " while (!userInput.matches(("[a-zA-Z\\s]+"))) { System.out.println("Numbers and special characters are not allowed, please try again"); userInput = scanner.nextLine(); } // Splits sentence into words, then modifies the words based on first letter String[] split = userInput.split("\\s"); for (int i = 0; i < split.length; i++) { if (split[i].startsWith("a")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("A")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("e")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("E")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("i")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("I")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("o")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("O")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("u")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("U")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("y")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else if (split[i].startsWith("Y")) { split[i] = split[i] + "ay "; System.out.print(split[i].toLowerCase()); } else { split[i] = split[i].substring(1) + split[i].charAt(0) + "ay"; System.out.print(split[i].toLowerCase() + " "); } } // Asks if user want to translate another word/sentence and looping the application System.out.println(" "); System.out.println("Do you want to translate another word/sentence?(y/n)"); String translateAgain = scanner.nextLine(); if (!translateAgain.equals("y")) { break; } } } }
问题分析与修复
核心问题
userInput变量仅在程序启动时读取一次,后续循环始终复用这个初始值,导致每次都翻译首次输入的内容,没有重新获取新输入。
修复方案
将输入读取逻辑移到while (true)循环内部,确保每次循环都能获取新的用户输入:
- 把初始的输入提示和
userInput读取代码移到循环开头 - 移除循环外的重复代码
优化后的完整代码
同时优化了冗余的大小写判断、完善了辅音翻译规则(匹配你描述的“移动第一个元音前所有辅音”)、提升了用户输入容错性:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Have fun using my Pig Latin translator!"); System.out.println("Those are the rules:"); System.out.println("Words that start with a vowel (A, E, I, O, U) simply have \"ay\" appended to the end of the word. "); System.out.println("Words that start with a consonant have all consonant letters up to the first vowel moved to the end of the word, and \"-ay\" is appended. -('Y' is counted as a vowel in this context). "); // Loop to translate another word/sentence while (true) { // 每次循环重新提示并获取新输入 System.out.println("Write a sentence or word to translate into Pig Latin!"); String userInput = scanner.nextLine(); // 验证输入格式 while (!userInput.matches("[a-zA-Z\\s]+")) { System.out.println("Numbers and special characters are not allowed, please try again"); userInput = scanner.nextLine(); } // 分割句子为单词并翻译 String[] split = userInput.split("\\s"); for (String word : split) { String lowerWord = word.toLowerCase(); char firstChar = lowerWord.charAt(0); String translated; // 判断是否以元音开头(含Y) if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || firstChar == 'o' || firstChar == 'u' || firstChar == 'y') { translated = lowerWord + "ay"; } else { // 找到第一个元音的位置 int vowelPos = 0; while (vowelPos < lowerWord.length()) { char c = lowerWord.charAt(vowelPos); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') { break; } vowelPos++; } // 移动辅音到末尾并加ay translated = lowerWord.substring(vowelPos) + lowerWord.substring(0, vowelPos) + "ay"; } System.out.print(translated + " "); } // 询问是否继续翻译 System.out.println("\nDo you want to translate another word/sentence?(y/n)"); String translateAgain = scanner.nextLine(); if (!translateAgain.equalsIgnoreCase("y")) { break; } } } }
关键优化点
- 简化元音判断:统一转小写后判断,避免重复的大小写分支
- 完善辅音翻译逻辑:实现了“移动第一个元音前所有辅音”的规则,而非仅移动第一个字符
- 容错性提升:用
equalsIgnoreCase判断是否继续,支持用户输入Y/y
内容的提问来源于stack exchange,提问作者Pikers Programista
相关产品推荐
相关产品推荐

