莫尔斯$ C $ D转换器 [英] Morse Code Converter

查看:116
本文介绍了莫尔斯$ C $ D转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经张贴在这个话题,但有相当(这话题被提上hold..so我不能编辑)改变了我的code(我试了一下其中一个用户在不同的变化说,但无豆)。我已经试过只运行toMorse,不过虽然它编译我没有得到任何输出和'java.lang.ArrayIndexOutOfBoundsException(在projmorsejava:22和46)的错误消息我不太确定如何配置toEnglish,在这一点上,我使用的replaceAll,和的indexOf方法的valueOf尝试。我使用plaintextString也试过,但也没有工作(我可能会错误地实现它)。
这里是我的修订code:
    进口java.util.Scanner中;

 公共类ProjMorse
{
公共静态无效的主要(字串[] args){
    扫描仪输入=新的扫描仪(System.in);    的String []阿尔法= {A,B,C,D,E,F,G,H,I,J,
            K,升,M,N,O,P,Q,R,S,T,U,V,
            W,X,Y,Z,1,2,3,4,5,6,7,8,
            9,0,,};
    串[]蒂= {.-,-...,-.-。, - 。。,,..-。, - ,
            ...,...,---,-.-,.- .., - , - , - ,。 - 。
            --.-,.-。,..., - ,..-,...-, - ,-..-,
            -.--, - ..,----,..---,...--,....-,... ..,
            -....,--...,--- ..,----,-----,| };    System.out的
            .println(进入英语从英语或转换莫尔斯从莫尔斯转换:);    串ANS = input.nextLine();    如果(ans.equals(英语)){
        的System.out.println(请输入您想转换为莫尔斯code文字:);
        串英文= input.nextLine();        的char [] =转换(english.toLowerCase())toCharArray()。
        的System.out.println(toMorse(转换,多蒂));在方法toMorse //调用            }
                    否则如果(ans.equals(莫尔斯)){
        System.out的
                .println(请输入文字,你想转换为英语(用不同的词'|'));
        字符串code = input.nextLine();         的String [] =转换(code.split([|],0));
                 的System.out.println(toEnglish(转换,阿尔法)); //调用方法toEnglish    }
其他
的System.out.println(无效的输入,请再试一次。);
 }   公共静态字符串toMorse(的char []编译的,的String []蒂)
   {
  字符串莫尔斯=;
  对于(INT J = 0; J< translates.length; J ++)
  {
    所以char a =转换[J]。
    如果(Character.isLetter(a))的
    {
       莫尔斯=蒂[A +'一'];
    }
  }
  回到莫尔斯; / *所以我尝试运行的只有这个(注释其他的东西了),它编译,但虽然它跑它没有翻译* /
}公共静态字符串toEnglish(的String []转换,字符串[]阿尔法)
{
  字符串s;
  对于(INT N = 0; N< translates.length; N ++)
  {
    字符串转换= [N];
            S = java.util.Arrays.asList(阿尔法)(Character.toChars(A +'A')[0]); //我不知道这里做什么..
  }
  返回S;
}
}


解决方案

有几件事情,不工作:


  1. 缺少初始公共类... (我怀疑是复制粘贴错误)

  2. 缺少进口java.util.Scanner中; ,因为您使用扫描仪

  3. 错误调用 toMorse :第一,被认为的两个ARGS(检查方法签名);那么,第一个参数的名字是不是莫尔斯(未知符号),必须转换,你刚才创建!关于第二:你需要的字符串数组是在地方例如E莫尔斯code,所以第二个参数必须是...蒂

  4. toEnglish 打错的电话:需要两个ARGS(检查方法签名,因为以前做过!);第一,必须是转换,再次(未知取值,因为莫尔斯之前)!关于第二个论点:见上面,但当然,因为你在输入有莫氏,你需要其他的阵列

  5. toMorse 您使用挑选每个字符翻译[J] ,而不是的valueOf

  6. 同样,在 toEnglish 您使用得到字符串翻译[N]

  7. 在两个 toEnglish toMorse ,你要积累转换后的字符串转换为字符串,并返回它在最后,即在环外!

  8. 有一个字符串,你选择的字符在pos 0使用 .charAt(0):执行像 x上的运算 - A x不能是一个字符串,所以既然你有一个字符串,你必须得到它的第一个字符。

  9. 注意的:你使用莫尔斯取值:在变量名法无关与呼叫者变量名!

举例 toMorse

 字符串莫尔斯=;
    对于(INT J = 0; J< translates.length; J ++)
    {
        所以char a =转换[J]。
        如果(Character.isLetter(a))的
        {
            莫尔斯+ =蒂[A - '一'];
        }
    }
    返回莫尔斯; //这个符号是未知的呼叫者

您将与一些简单称其为

 的System.out.println(toMorse(转换,多蒂));

toEnglish 的解决方法是非常相似(虽然你原来的code将无法正常工作),我希望你可以自己做。

我想就是全部,更多或更少。

建议修复toEnglish:

有关你在你的转换数组有每个字符串,你必须寻找到字符串数组以搜索点和线的特定序列。

如果你找到它,你找到它的位置(索引),这也是你保持字母数组中的右侧字母的索引。所以,你可能需要向阵列添加作为参数,使用索引来接信。

或者说,你可以用你习惯 toMorse 相同的绝招,并保持 toEnglish 方法签名不变:一旦你的指标,既然你找到了,你可以反转的编码算法,因此你必须添加'A'来的指数(整数)获得code为信。

您可以使用类似 Character.toChars(K +'A')[0] 把要添加到积累字符串炭,是 K 的索引。

繁琐的符号 Character.toChars(K +'A')[0] 是因为确实 Character.toChars 返回一个数组,所以我们挑选阵列,这是我们所需要的字符的第一个元素。

I already posted a topic on this but have considerably (and that topic was put on hold..so I can't edit) changed my code (I tried what one of the users said in different variations, but no beans). I've tried running just toMorse, but although it compiles I get no output and an error message of 'java.lang.ArrayIndexOutOfBoundsException(at projmorsejava:22 and 46)' I'm not sure how to configure toEnglish, at this point I've tried using the replaceAll, indexOf and valueOf methods. I've also tried using plaintextString but that also did not work out (I may have implemented it incorrectly). Here is my revised code: import java.util.Scanner;

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

    String[] alpha = { "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[] dottie = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
            "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
            "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
            "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
            "-....", "--...", "---..", "----.", "-----", "|" };

    System.out
            .println("Enter English to convert from English or Morse to convert from Morse:");

    String ans = input.nextLine();

    if (ans.equals("English")) {
        System.out.println("Please enter the text you would like   to convert to Morse Code: ");
        String english = input.nextLine();

        char[] translates = (english.toLowerCase()).toCharArray();
        System.out.println(toMorse(translates, dottie)); //calls on method toMorse

            }
                    else if (ans.equals("Morse")) {
        System.out
                .println("Please enter the text you would like to convert to English (separate words with '|'):");
        String code = input.nextLine();

         String[] translates = (code.split("[|]", 0));
                 System.out.println(toEnglish(translates, alpha));//calls on method toEnglish

    }
else
System.out.println("Invalid input, please try again.");
 }

   public static String toMorse(char [] translates, String [] dottie)
   {            
  String morse = "";
  for (int j = 0; j < translates.length; j++)
  {
    char a = translates[j];
    if(Character.isLetter(a))
    {
       morse = dottie[a + 'a'];
    }
  }
  return morse;/*so I tried running only this(commented other stuff out) and it compiled but although it ran it didnt translate */
}

public static String toEnglish(String [] translates, String  [] alpha)
{
  String s;
  for (int n = 0; n < translates.length; n++)
  {
    String a = translates[n];
            s = java.util.Arrays.asList(alpha).(Character.toChars(a + 'a')[0]);//I'm not sure what to do here..
  }
  return s;
}
}

解决方案

There are several things that do not work:

  1. missing initial public class ... (I suspect a copy-paste error)
  2. missing import java.util.Scanner; since you use Scanner
  3. wrong call to toMorse: first, two args are expected (check the method signature); then, first argument's name is not morse (unknown symbol), it must be translates, which you have just "created"! About the second: you need the array of strings that has in place of the e.g. "e" the morse code, so second argument must be... dottie
  4. wrong call to toEnglish: two args required (check the method signature, as done before!); the first, has to be translates, again (unknown s, as morse before)! About second argument: see above but of course since you have morse in input, you need the other array.
  5. in toMorse you pick each char using translates[j], not valueOf.
  6. similarly, in toEnglish you get the string using translates[n]
  7. in both toEnglish and toMorse, you have to "accumulate" the converted string into a string, and return it at the end, i.e. outside the loops!
  8. having a string, you pick the char at pos 0 using .charAt(0): to perform an op like x - 'a' x can't be a string, so since you have a string, you must get its first char.
  9. Note: about your using of morse and s: variable names in a method has nothing to do with variable names in the caller!

Example for toMorse:

    String morse = "";
    for (int j = 0; j < translates.length; j++)
    {
        char a = translates[j];
        if(Character.isLetter(a))
        {
            morse += dottie[a - 'a'];
        }
    }
    return morse;  // this symbol is unknown to the caller

You will call it with something as simple as

              System.out.println(toMorse(translates, dottie));

The fix for toEnglish is very similar (though your original code won't work), I hope you can do it by yourself.

I think is all, more or less.

Suggestion to fix toEnglish:

For each string you have in your translates array, you have to search into dottie string array in order to search for that particular sequence of dots and lines.

If you find it, the position (index) you find it at, it's also the index of the right letter you keep in the alpha array. So you might need to add that array as argument and use the index to pick the letter.

Or, rather, you can use the same "trick" you used to toMorse and keep the toEnglish method signature unchanged: once you have the index, since you have found it, you can "invert" the coding algorithm, so you have to add 'a' to that index (integer) to obtain the code for the letter.

You can use something like Character.toChars(k + 'a')[0] to take the char you want to add to the "accumulation string", being k the index.

The cumbersome notation Character.toChars(k + 'a')[0] is since indeed Character.toChars returns an array, so we pick the first element of that array, which is the char we need.

这篇关于莫尔斯$ C $ D转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆