作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我已经发布了一个关于此的主题但是有相当大的(并且该主题被搁置..所以我无法编辑)更改了我的代码(我尝试了其中一位用户在不同变体中所说的,但没有 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;
}
}
我是一名优秀的程序员,十分优秀!