对于我的Java类简介,我应该创建一个摩尔斯电码翻译器,可以将英语翻译成莫尔斯语,也可以翻译成莫尔斯语。我的代码适用于将英语转换为莫尔斯语,但似乎无法实现其他方式,从摩尔斯电码到英语。这是提示:
该项目涉及编写一个程序,将摩尔斯电码翻译成英文和 英语到莫尔斯电码。您的程序将提示用户指定 所需的翻译类型,输入一串莫尔斯电码字符或英文 字符,然后显示翻译的结果。 输入摩尔斯电码时,用一个空格分隔每个字母/数字,和 用“|”分隔多个单词。例如, - --- | - ......将是莫尔斯 句子“是”的代码输入。您的程序只需要处理一个 句子并且可以忽略标点符号。 输入英语时,用空格分隔每个单词。
我想知道我现在的摩托车代码""码。如果您了解出了什么问题,请帮忙。我花了好几个小时试图解决这个问题,并且我已经在2015年8月26日太平洋时间午夜之前完成了这个工作。谢谢! 这是我的代码:
//Justin Buckley
//8.26.2015
import java.util.Scanner;
public class MorseCodeProject1 {
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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
Scanner input = new Scanner (System.in);
System.out.println( "Please enter \"MC\" if you want to translate Morse Code into English, or \"Eng\" if you want to translate from English into Morse Code" );
String a = input.nextLine();
if ( a.equalsIgnoreCase("MC"))
{
System.out.println( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
String b = input.nextLine();
String[] words = b.split("|");
for (String word: words )
{
String[] characters = word.split(" ");
for (String character: characters)
{
if (character.isEmpty()) { continue; }
for (int m = 0; m < Morse.length; m++)
{
if (character.equals(Morse[m]))
System.out.print(English[ m ]);
}
}
System.out.print(" ");
}
}
else if ( a.contains("Eng" ))
{
System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
String c = input.nextLine();
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" );
}
}
}
答案 0 :(得分:0)
继续发表评论:在提出这类问题之前,你真的需要做好功课。
所有答案都可以在Java教程中找到:https://docs.oracle.com/javase/tutorial/java/data/strings.html
更具体地说,您想知道如何操作字符串:https://docs.oracle.com/javase/tutorial/java/data/manipstrings.html
这里的教程是一个基本的例子,展示了如何实现你想要的目标:
if ( a.equalsIgnoreCase("MC"))
{
System.out.println( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
//Example input string
String b = ".... . .-.. .-.. --- | .-- --- .-. .-.. -.. | .. -. | -- --- .-. ... ." ;
//We cant use the split method here because it removes blank spaces, so lets keep it basic:
//String[] words = b.split("|");
int letterStart = 0;
int letterEnd = 0;
String letter = "";
while (letterStart < b.length())
{
letterEnd = b.indexOf(" ", letterStart);
//check if end of string has been reached
if (letterEnd == -1)
{
//grab last morse letter/sequence
letter = b.substring(letterStart);
}
else
{
//grab next morse letter/sequence
letter = b.substring(letterStart, letterEnd);
}
//convert each morse sequence to english
for (int m = 0; m < Morse.length; m++)
{
if (letter.equals("|"))
{
//show spaces between words
System.out.print(" ");
break;
}
else if (letter.equals(Morse[m]))
{
//show english letter
System.out.print(English[m]);
break;
}
}
if (letterEnd == -1)
{
break;
}
letterStart = letterEnd+1;
}
}