1

我对java非常陌生,并且在我的莫尔斯电码程序上遇到了麻烦。请耐心等待,但是当输入要从莫尔斯电码翻译成英文的文本时,它只会打印 null 来代替字母。任何帮助将不胜感激。

import java.util.Scanner;

public class MorseCode
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("To convert English to Morse Code, type M. To convert Morse Code to English, type E.");

        String cType = Input.nextLine();

        String type = cType.toLowerCase();

        if("m".equals(type))
        {
            String eng;
            System.out.println("Please enter the English text to be translated.");
            eng = Input.nextLine();
            EToM(eng);
        }
        else
        {
            String morse;
            System.out.println("Please enter the Morse code text to be translated, with multiple words seperated by a |.");
            morse = Input.nextLine();
            MToE(morse);
        }
    }
    public static void EToM(String eng)
    {
        String EToMList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};
        String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
        String translation[] = new String[eng.length()];

        for(int x = 0; x < eng.length(); x++)
        {
            for(int y = 0; y < alphabet.length(); y++)
            {
                if(eng.charAt(x) == alphabet.charAt(y))
                {
                    translation[x] = EToMList[y];
                }
            }
        }

        System.out.println("Your translated message is:");

        for(int z = 0; z < eng.length(); z++)
        {
            System.out.println(translation[z]); 
        }
    }

    public static void MToE(String morse)
    {
        int arraySize = 0;
        String MToEList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};
        String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
        char space = '|';

        for(int x = 0; x < morse.length(); x++)
        {
            if(morse.charAt(x) == space)
            {
                arraySize += 1;
            }
        }

        String segmentedMessage[] = new String[arraySize];
        String translation[] = new String[arraySize];

        int a = 1;
        int counter = 0;

        for(int y = 0; y < morse.length(); y++)
        {
            if(morse.charAt(y) == space)
            {
                segmentedMessage[counter] = morse.substring((a - 1), (y + 1));
                a = y;
            }
        }

        for(int z = 0; z < segmentedMessage.length; z++)
        {
            for(int i = 0; i < alphabet.length(); i++)
            {
                if(segmentedMessage[z] == MToEList[i])
                {
                    translation[z] = alphabet.substring(i - 1, i + 1);
                }
            }
        }

        System.out.println("Your translated message is:");

        for(int j = 0; j < translation.length; j++)
        {
            System.out.println(translation[j]);
        }
    }
}       
4

2 回答 2

1

我认为你的问题在于

if(segmentedMessage[z] == MToEList[i])

前面看到您在比较 char 类型,char 是一种原始类型,这意味着您可以使用 == 来安全地比较值。在我提到的那一行,您正在比较字符串类型。字符串是一个 java 对象。

所有 Java 对象都应该使用 .compare 而不是 ==

例子

char char1 = 'c';
char char2 = 'c';
char1 == char2 // true!

String str1 = "c";
String str2 = "c";
str1 == str2 // not always going to be true, even tho it would seem that way
str1.equals(str2) // true!
于 2013-08-16T18:21:41.470 回答
1

我看到的第一个问题是在您的 MToE 方法中,在本节中:

int a = 1;
int counter = 0

for(int y = 0; y < morse.length(); y++)
{
    if(morse.charAt(y) == space)
    {
        segmentedMessage[counter] = morse.substring((a - 1), (y + 1));
        a = y;
    }
}

您永远不会增加计数器,因此只有 segmentedMessage 的第一个元素会被莫尔斯字符串填充。如果你得到一个字符,然后是一堆空值,这可能是你的问题。

此外,在此行中以相同的方法:

translation[z] = alphabet.substring(i - 1, i + 1);

您正在使用由字母表中的第 i 个和第 i 个字符组成的两个元素子字符串。看起来你的意思是alphabet.substring(i,i + 1)。特别是,如果 i = 0,这将导致问题,因为您将尝试从 -1 开始获取子字符串。

于 2013-08-16T18:21:52.233 回答