我已经发布了一个关于此的主题,但有相当多(并且该主题被搁置......因此我无法编辑)更改了我的代码(我尝试了其中一个用户在不同的变体中说过,但没有bean) 。我试过运行只是为了马,但是虽然它编译但我没有输出和'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;
}
}
答案 0 :(得分:1)
有些事情不起作用:
public class ...
(我怀疑是复制粘贴错误)import java.util.Scanner;
toMorse
的错误调用:首先,需要两个args(检查方法签名);那么,第一个参数的名称不是morse
(未知符号),它必须是translates
,你刚刚创建了#{1};关于第二个:你需要代替例如字符串的字符串数组。 &#34; E&#34;莫尔斯代码,所以第二个参数必须是... dottie toEnglish
的错误调用:需要两个args(检查方法签名,如前所述!);第一个,必须再次translates
(未知s
,之前为morse
)!关于第二个参数:见上文但当然因为你在输入中有莫尔斯,你需要另一个数组。toMorse
中,您使用translates[j]
选择每个字符,而不是valueOf
。toEnglish
中,您可以使用translates[n]
toEnglish
和toMorse
中.charAt(0)
在pos 0选择字符:执行类似x - 'a'
的操作x不能是一个字符串,所以既然你有一个字符串,你必须得到它的第一个字符。morse
和s
:方法中的变量名称与调用者中的变量名称无关! toMorse
的示例:
String morse = "";
for (int j = 0; j < translates.length; j++)
{
char a = translates[j];
if(Character.isLetter(a))
{
morse += dottie[a - 'a'];
}
}
return morse; // this symbol is unknown to the caller
你会用一些简单的东西来称呼它
System.out.println(toMorse(translates, dottie));
toEnglish
的解决方案非常相似(虽然您的原始代码无法正常工作),但我希望您可以自行完成。
我认为是全部,或多或少。
对于translates
数组中的每个字符串,您必须搜索dottie
字符串数组,以便搜索特定的点和线序列。
如果您找到它,您找到它的位置(索引),它也是您在alpha
数组中保留的正确字母的索引。因此,您可能需要将该数组添加为参数,并使用索引来选择该字母。
或者说,你可以使用相同的&#34;技巧&#34;您曾经toMorse
并保持toEnglish
方法签名不变:一旦您拥有索引,因为您已找到它,您可以&#34;反转&#34;编码算法,因此您必须将'a'
添加到该索引(整数)以获取该字母的代码。
您可以使用Character.toChars(k + 'a')[0]
之类的内容将要添加的字符添加到&#34;累积字符串&#34;,为k
索引。
繁琐的符号Character.toChars(k + 'a')[0]
因为确实Character.toChars
返回一个数组,所以我们选择该数组的第一个元素,这是我们需要的char。
答案 1 :(得分:0)
重要的是,您自己解决这些错误,以了解编程所需的必要调试技巧。这可能不是你想要的,但是:
"java how to (enter what your trying to do here)"
那里有大量的信息。选择要搜索的内容时,请确保将其细分为最基本的格式,例如"java how to convert integer to string"