该项目涉及编写一个程序,将摩尔斯电码翻译成英语和将英语翻译成摩尔斯电码。您的程序应提示用户指定所需的翻译类型,输入一串摩尔斯电码字符或英文字符,然后显示翻译结果。
输入摩尔斯电码时,每个字母/数字用一个空格分隔,多个单词用“|”分隔。例如 - ---- | -…… 将是句子“to be”的摩尔斯电码输入。你的程序只需要处理一个句子,可以忽略标点符号。
输入英文时,每个单词用空格隔开。
我得到了一个无与伦比的类型: if ( Morse [ m ] == b.charAt ( m ) ) 行中的字符串和字符错误。有想法该怎么解决这个吗?谢谢!
public class project {
public static void main ( String [] args ) {
char [] English = { '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 [] Morse = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
String a = Input.getString ( "Please enter \"MC\" if you want to translate Morse Code into English, or \"Eng\" if you want to translate from English into Morse Code" );
if ( a == "MC" )
{
String b = Input.getString ( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
for ( int x = 0; x < Morse.length; x++ )
{
for ( int m = 0; m < b.length (); m++ )
{
if ( Morse [ m ] == b.charAt ( m ) )
System.out.print ( English [ m ] + " " );
}
}
}
else if ( a == "Eng" )
{
String c = Input.getString ( "Please enter a sentence in English, and separate each word with a blank space." );
c = c.toLowerCase ();
for ( int x = 0; x < English.length; x++ )
{
for ( int y = 0; y < c.length (); y++ )
{
if ( English [ x ] == c.charAt ( y ) )
System.out.print ( Morse [ x ] + " " );
}
}
}
else
{
System.out.println ( "Invalid Input" );
}
}
}