问题
For my Intro to Java class I'm supposed to create a Morse code translator that can convert both from English to Morse and from Morse to English. My code works for converting English to Morse, but can't seem to manage to go the other way, from Morse Code to English. Here is the prompt:
This project involves writing a program to translate Morse Code into English and English into Morse Code. Your program shall prompt the user to specify the desired type of translation, input a string of Morse Code characters or English characters, then display the translated results. When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. For example, - --- | -… . would be the Morse Code input for the sentence “to be”. Your program only needs to handle a single sentence and can ignore punctuation symbols. When inputting English, separate each word with a blank space.
I am wondering what's wrong with my current "Morse code" code. Please help if you understand what's going wrong, I've spent hours trying to figure this thing out and I've got to this before midnight Pacific Time on 8/26/2015. Thanks! Here is my code:
//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" );
}
}
}
回答1:
here this code will work for you, you had your english to morse code wrong too i fixed that too
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 = null;
if(b.contains("|")){
words = b.split("[|]");
}else{
words = new String[1];
words[0] = b;
}
for (String word: words )
{
String[] characters = word.split(" ");
for(int h = 0;h < characters.length;h++){
for(int i = 0;i < Morse.length;i++){
if(characters[h].equals(Morse[i])){
System.out.print(English[i]);
}
}
}
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 < c.length(); x++ )
{
for ( int y = 0; y < English.length; y++ )
{
if ( English [ y ] == c.charAt ( x ) )
System.out.print ( Morse [ y ] + " " );
}
}
}
else
{
System.out.println ( "Invalid Input" );
}
}
回答2:
Following on from my comment: You really really need to do your homework before asking this sort of question.
All the answers can be found in the Java tutorial: https://docs.oracle.com/javase/tutorial/java/data/strings.html
More specifically you want to know how to Manipulating a string: https://docs.oracle.com/javase/tutorial/java/data/manipstrings.html
In keeping with the tutorials here is a basic example showing how you can achieve what you want:
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;
}
}
来源:https://stackoverflow.com/questions/32239768/need-assistance-with-java-morse-code-translator-quickly