摩尔斯电码转换器:编译错误

时间:2013-08-14 22:59:37

标签: java compiler-errors converter

我目前正致力于双向摩尔斯电码翻译程序。我还没完成,但在对程序进行测试时,我收到了多个编译错误。我不知道出了什么问题,但我确定它很简单。任何帮助将不胜感激。

到目前为止,这是我的代码:

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 个答案:

答案 0 :(得分:3)

以下是我发现的问题:

1

if(message.charAt(x) = "|")

应该是

if(message.charAt(x) == '|')

if(message.charAt(k) = "|")也是如此。回想一下,=赋值==比较charAt()返回char而不是String '|',因此我们与"|"进行比较,而不是.equals()(无论如何,您应该使用segementedMessage[y] = message.substirng(j, k); 作为字符串。)


2

substring

segmentedMessage在这里是错误的,String translation[] = new String[segments] 也是如此(您之前使用正确的拼写声明)。


3

;

你在这一行的末尾错过了case "y-.--:


4

"

您错过了该字符串末尾的结尾case "y-.--":(应为{{1}})。