我目前正在研究一种双向莫尔斯电码翻译程序。我还没完成,但是在测试程序时收到了多个编译错误。我不知道出什么问题了,但可以肯定这很简单。任何帮助,将不胜感激。
到目前为止,这是我的代码:
public class MorseCodeProject
{
public static void main(String[] args)
{
System.out.println("Please enter the conversion to be preformed.");
System.out.println("Type 1 to convert English to Morse code.");
System.out.println("Type 2 to convert Morse code to English.");
int type = Input.getInt();
System.out.println("Please enter the message to be converted.");
String message = Input.getString();
if(type == 1)
EtoM(message);
else
MtoE(message);
}
public static void EtoM(String message)
{
String translation[] = new String[message.length()];
String y;
for(int x = 0; x < message.length(); x++)
{
y = message.substring(x, x + 1);
switch(y)
{
case "a":
translation[x] = ".-";
break;
case "b":
translation[x] = "-...";
break;
case "c":
translation[x] = "-.-.";
break;
case "d":
translation[x] = "-..";
break;
case "e":
translation[x] = ".";
break;
case "f":
translation[x] = "..-.";
break;
case "g":
translation[x] = "--.";
break;
case "h":
translation[x] = "....";
break;
case "i":
translation[x] = "..";
break;
case "j":
translation[x] = ".---";
break;
case "k":
translation[x] = "-.-";
break;
case "l":
translation[x] = ".-..";
break;
case "m":
translation[x] = "--";
break;
case "n":
translation[x] = "-.";
break;
case "o":
translation[x] = "---";
break;
case "p":
translation[x] = ".--.";
break;
case "q":
translation[x] = "--.-";
break;
case "r":
translation[x] = ".-.";
break;
case "s":
translation[x] = "...";
break;
case "t":
translation[x] = "-";
break;
case "u":
translation[x] = "..-";
break;
case "v":
translation[x] = "...-";
break;
case "w":
translation[x] = ".--";
break;
case "x":
translation[x] = "-..-";
break;
case "y":
translation[x] = "-.--";
break;
case "z":
translation[x] = "--..";
break;
case "1":
translation[x] = ".----";
break;
case "2":
translation[x] = "..---";
break;
case "3":
translation[x] = "...--";
break;
case "4":
translation[x] = "....-";
break;
case "5":
translation[x] = ".....";
break;
case "6":
translation[x] = "-....";
break;
case "7":
translation[x] = "--...";
break;
case "8":
translation[x] = "---..";
break;
case "9":
translation[x] = "----.";
break;
case "0":
translation[x] = "-----";
break;
case " ":
translation[x] = "|";
default:
break;
}
for(int z = 0; z < message.length(); z++)
{
System.out.println(translation[z]);
}
}
}
public static void MtoE(String message)
{
int segments = 1;
char segment1;
int x = 0;
for(x = 0; x < message.length(); x++);
{
if(message.charAt(x) = "|")
{
segments += 1;
}
}
String segmentedMessage[] = new String[segments];
int j = 0;
int k;
for(int y = 0; y < segments; y++)
{
for(k = j; k < message.length(); k++)
{
if(message.charAt(k) == "|")
{
segementedMessage[y] = message.substirng(j, k);
j = k;
}
}
}
String segment;
String translation[] = new String[segments]
for(int c = 0; c < segments; c++)
{
segment = segmentedMessage[c];
switch(segment)
{
case ".-":
translation[c] = "a";
break;
case "-...":
translation[c] = "b";
break;
case "-.-.":
translation[c] = "c";
break;
case "-..":
translation[c] = "d";
break;
case ".":
translation[c] = "e";
break;
case "..-.":
translation[c] = "f";
break;
case "--.":
translation[c] = "g";
break;
case "....":
translation[c] = "h";
break;
case "..":
translation[c] = "i";
break;
case ".---":
translation[c] = "j";
break;
case "-.-":
translation[c] = "k";
break;
case ".-..":
translation[c] = "l";
break;
case "--":
translation[c] = "m";
break;
case "-.":
translation[c] = "n";
break;
case "---":
translation[c] = "o";
break;
case ".--.":
translation[c] = "p";
break;
case "--.-":
translation[c] = "q";
break;
case ".-.":
translation[c] = "r";
break;
case "...":
translation[c] = "s";
break;
case "-":
translation[c] = "t";
break;
case "..-":
translation[c] = "u";
break;
case "...-":
translation[c] = "v";
break;
case ".--":
translation[c] = "w";
break;
case "-..-":
translation[c] = "x";
break;
case "y-.--:
translation[c] = "y";
break;
case "--..":
translation[c] = "z";
break;
case ".----":
translation[c] = ".----";
break;
case "..--":
translation[c] = "..---";
break;
case "...--":
translation[c] = "...--";
break;
case "....-":
translation[c] = "....-";
break;
case ".....":
translation[c] = ".....";
break;
case "-....":
translation[c] = "-....";
break;
case "--...":
translation[c] = "--...";
break;
case "---..":
translation[c] = "---..";
break;
case "----.":
translation[c] = "----.";
break;
case "-----":
translation[c] = "-----";
break;
case "|":
translation[c] = " ";
default:
break;
}
}
}
}
最佳答案
这是我发现的问题:
1。
if(message.charAt(x) = "|")
应该是
if(message.charAt(x) == '|')
if(message.charAt(k) = "|")
也是如此。回想一下=
是赋值的,而==
是比较的,并且charAt()
返回的char
而不是String
,因此我们将其与'|'
而不是"|"
进行比较(无论如何,您都应将.equals()
用于字符串)。2。
segementedMessage[y] = message.substirng(j, k);
此处
substring
的拼写错误,以及segmentedMessage
(您之前用正确的拼写声明的)也错了。3。
String translation[] = new String[segments]
您在此行末尾缺少
;
。4。
case "y-.--:
您在该字符串的末尾缺少结尾的
"
(应该是case "y-.--":
)。