morse代码给英语翻译程序提供了数组索引

qjp7pelc  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(503)

我必须创造莫尔斯到英语,反之亦然翻译。英文到莫尔斯部分是有效的,但每当我尝试在莫尔斯输入一些东西时,它就会给我一个arrayindexoutofbounds异常,我一直在思考如何修复它。我已经加入了一个分割函数,但我不确定为什么会出现异常。

  1. public static void main(String[] args) throws IOException {
  2. Scanner input = new Scanner(System.in);
  3. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  4. char[] english = { 'a', 'b', 'c', 'd', 'e', 'f',
  5. 'g', 'h', 'i', 'j', 'k', 'l',
  6. 'm', 'n', 'o', 'p', 'q', 'r',
  7. 's', 't', 'u', 'v', 'w', 'x',
  8. 'y', 'z', '1', '2', '3', '4',
  9. '5', '6', '7', '8', '9', '0'};
  10. String[] morse = { ".-", "-...", "-.-.", "-..", ".",
  11. "..-.", "--.", "....", "..", ".---",
  12. "-.-", ".-..", "--", "-.", "---",
  13. ".--.", "--.-", ".-.", "...", "-",
  14. "..-", "...-", ".--", "-..-", "-.--",
  15. "--..", "|" }
  16. String userInput;
  17. int translatorChoice;
  18. String result;
  19. System.out.println("Enter 1 for English to Morse code. Enter 2 for Morse to English:");
  20. translatorChoice = input.nextInt();
  21. if (translatorChoice != 1 && translatorChoice !=2 ){
  22. throw new ArithmeticException("Please enter a valid number");
  23. }
  24. System.out.println();
  25. System.out.println("Please input sentence into the translator:");
  26. userInput = br.readLine();
  27. if (translatorChoice == 1){
  28. userInput = userInput.toLowerCase();
  29. englishtoMorse(morse,userInput,english);}
  30. else if(translatorChoice == 2) {
  31. morsetoEnglish(english, userInput, morse);}
  32. public static void morsetoEnglish (char[] english, String userInput, String[] morse){
  33. String[] input = userInput.split("|");
  34. for (int i = 0; i < input.length; i++){
  35. for (int j = 0; j < morse.length; i++){
  36. if (morse[j].equals(input[i])) {
  37. System.out.print(english[j]);
  38. }}}}
vohkndzv

vohkndzv1#

这是工作代码。要做3个改变,首先是分拆 "|" 必须更改为正确的正则表达式 "\\|" 分道扬镳 '|' . split() 将正则表达式作为参数 '|' 是正则表达式中的特殊字符,因此需要使用正则表达式的 \ ,但是,当将正则表达式转换为java字符串时,您会再次转义它。结果是 \\| 第二,内部 for 当字符串匹配时,循环可以停止,因此添加了break。
第三,变量 i 改为 j 在内部 for 循环增量。

  1. public static void morsetoEnglish (char[] english, String userInput, String[] morse){
  2. String[] input = userInput.split("\\|");
  3. for (int i = 0; i < input.length; i++){
  4. for (int j = 0; j < morse.length; j++){
  5. if (morse[j].equals(input[i])) {
  6. System.out.print(english[j]);
  7. break;
  8. }
  9. }
  10. }
  11. }

以下是一些测试输入/输出:

  1. Enter 1 for English to Morse code. Enter 2 for Morse to English:
  2. 2
  3. Please input sentence into the translator:
  4. .--.|.|-|
  5. pet
展开查看全部
wooyq4lh

wooyq4lh2#

所以,我认为这需要一些逻辑上的重新工作

  1. public static void englishToMorse(char[] english, String userInput, String[] morse){
  2. char[] chars = userInput.toCharArray();
  3. String englishCharsAsString = new String(english);
  4. for (char character: chars) {
  5. if (character == ' ') {
  6. System.out.println(" ");
  7. continue;
  8. }
  9. int index = englishCharsAsString.indexOf(character);
  10. System.out.println(morse[index]);
  11. }
  12. }

让我解释一下这里发生了什么,我们也应该能够对其进行反向工程,以使 morseToEnglish 方法。
所以我们首先把用户的输入转换成一个字符数组,这样我们就知道用户的单个字符是什么。
接下来,我们将英文字符数组转换为一个字符串,这样我们可以找到该字符串中字母的索引,并使用该索引将其Map到摩尔斯电码数组
现在我们循环遍历用户输入中的所有字符,在英文字符串中找到该字符的索引,然后使用该索引获取莫尔斯电码表示。

展开查看全部

相关问题