gpt4 book ai didi

java - 摩尔斯电码转换器

转载 作者:行者123 更新时间:2023-11-30 11:23:54 25 4
gpt4 key购买 nike

<分区>

我已经发布了一个关于此的主题但是有相当大的(并且该主题被搁置..所以我无法编辑)更改了我的代码(我尝试了其中一位用户在不同变体中所说的,但没有 bean ) .我试过只运行 toMorse,但尽管它编译了,但我没有得到任何输出和错误消息 'java.lang.ArrayIndexOutOfBoundsException(at projmorsejava:22 and 46)' 我不确定如何配置为英语,此时我尝试使用 replaceAll、indexOf 和 valueOf 方法。我也尝试过使用 plaintextString 但也没有成功(我可能实现不正确)。这是我修改后的代码:

import java.util.Scanner;

public class ProjMorse
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String[] alpha = { "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", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", " " };
String[] dottie = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|" };

System.out
.println("Enter English to convert from English or Morse to convert from Morse:");

String ans = input.nextLine();

if (ans.equals("English")) {
System.out.println("Please enter the text you would like to convert to Morse Code: ");
String english = input.nextLine();

char[] translates = (english.toLowerCase()).toCharArray();
System.out.println(toMorse(translates, dottie)); //calls on method toMorse

}
else if (ans.equals("Morse")) {
System.out
.println("Please enter the text you would like to convert to English (separate words with '|'):");
String code = input.nextLine();

String[] translates = (code.split("[|]", 0));
System.out.println(toEnglish(translates, alpha));//calls on method toEnglish

}
else
System.out.println("Invalid input, please try again.");
}

public static String toMorse(char [] translates, String [] dottie)
{
String morse = "";
for (int j = 0; j < translates.length; j++)
{
char a = translates[j];
if(Character.isLetter(a))
{
morse = dottie[a + 'a'];
}
}
return morse;/*so I tried running only this(commented other stuff out) and it compiled but although it ran it didnt translate */
}

public static String toEnglish(String [] translates, String [] alpha)
{
String s;
for (int n = 0; n < translates.length; n++)
{
String a = translates[n];
s = java.util.Arrays.asList(alpha).(Character.toChars(a + 'a')[0]);//I'm not sure what to do here..
}
return s;
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com