首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Morse代码应用程序(JAVA Gui)

Morse代码应用程序(JAVA Gui)
EN

Stack Overflow用户
提问于 2015-05-16 13:18:10
回答 3查看 428关注 0票数 1

我正在尝试创建一个gui应用程序来读取英语用户的输入并翻译为morse (http://ascii-table.com/morse-code.php)。到目前为止,我已经触及了这个项目的基本部分。我的问题是:读摩尔斯最好的方式是什么?我应该创建一个文本文件来导入莫尔斯字母,还是应该在程序中声明每一个字母以进行翻译?下一个问题是我该怎么做?如果可能的话,请参考教程。耽误您时间,实在对不起。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-16 13:32:57

由于Morse代码不太可能更改,因此硬编码字符到代码字符串的映射是一个有效的选项:

代码语言:javascript
复制
private static Map<Character,String> charToCode = new HashMap<Character,String>();
{
    charToCode.put('A', ".-");
    charToCode.put('B', "-...");
    ...
    charToCode.put('Z', "--..");
}

此映射允许您一次将消息转换为一个字符:

  • 为结果创建一个StringBuilder
  • 一次只遍历输入字符的字符。你可以使用charAt(i)
  • 将字符转换为大写
  • 使用charToCode.get(upperChar)查找字符的代码表示形式
  • 将表示附加到StringBuilder;在其后面追加一个空格
  • 循环结束后,将StringBuilder转换为String,并将其放在标签上。
票数 2
EN

Stack Overflow用户

发布于 2015-05-16 13:24:39

您可以在外部维护两个文件。一种是从信函到Morse代码的映射,另一种是从Morse代码到信函的映射。然后,您可以通过从相应的文件进行查找来构建两个转换器。

票数 1
EN

Stack Overflow用户

发布于 2017-06-30 10:17:29

您可以从用户读取输入,并在运行时将其与程序中存储的预定义字符和数字进行比较,也可以输入文件并使用FileReader读取。

但是下面是您应该在运行时比较中执行的逻辑

代码语言:javascript
复制
 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',
             ',', '.', '?' };   //Defining a Character Array of the English Letters numbers and Symbols so that we can compare and convert later 

     String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
                ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
                "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
                "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
                "-----", "--..--", ".-.-.-", "..--.." };

System.out.println("-->Enter the Sentence that you want to Transmit Using the Morse Code ");
        System.out.print("->");
        sentence = br.readLine();
        System.out.println("");
        sentence = sentence.toLowerCase(); //Because morse code is defined only for the lower case letters and the numbers and the Symbols will remain the Same
        char[] morsec = sentence.toCharArray();
        for(int i = 0; i < morsec.length;i++)  //The loop will run till i is less than the number of characters in the Sentence because Every Character needs to Be Converted into the Respective Morse Code 
        {//For Every Letter in the User Input Sentence
            for(int j = 0;j<english.length;j++)   //For Every Character in the morsec array we will have to traverse the entire English Array and find the match so that it can be represented 
            {
                if(english[j] == morsec[i])  //If the Character Present in English array is equal to the character present in the Morsec array then Only Execute 
                {//Always remember that the condition in the Inner loop will be the first to be Equated in the If Statement because that will change until the characters match 
                    answer = answer + morse[j] + " ";  //After Every Letter is generated in the Morse Code we will give a Space 
                }  //Since the Letters in the English char and the symbols present in the morse array are at the Same Index 
            }
        }
        System.out.println("-->The Morse Code Translation is:- ");
        System.out.Println(answer);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30276153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档