为我的编程课创建一个摩尔斯电码转换器,输出有问题



这是我在这个平台上的第一篇文章,我对这个Java编程有点陌生,也不擅长英语:p

我的老师要求一个莫尔斯电码翻译器,它可以对字母进行莫尔斯,反之亦然

这是我想出的代码:

import java.util.Scanner;
public class Morse2 {
    public static void main(String[] args){
 String[] letras = { "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"};                  
 String[] MORSE = {
 ".-" ,"-...","-.-.","-.." ,"." , 
 "..-.","--." ,"....",".." ,".---", 
 "-.-" ,".-..","--" ,"-." ,"---" , 
 ".--.","--.-",".-." ,"..." ,"-" , 
 "..-" ,"...-",".--", "-..-","-.--", 
 "--.."}; 
System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
Scanner in=new Scanner(System.in);
String frase =in.nextLine();
String resp="";
frase=frase.toLowerCase();
String[] paraletras=frase.split("");
String[]paraMorse=frase.split(" ");
for(int i=0;i< paraletras.length;i++){
    for(int j=0;j< letras.length ;j++){
     if (paraletras[i].equals(letras[j])){
    resp=resp+ MORSE[j]+" ";}
    }
}
for(int k=0;k<paraMorse.length;k++){
     for (int l=0;l<MORSE.length;l++){
          if(paraMorse[k].equals(MORSE[l])){
    resp=resp+letras[l]+ " ";}}
    }
System.out.print(resp);}
    }

该类编译良好,但我的输出存在一些问题,更具体地说是输出的顺序:

例如我的输入" a b -.- c " 我想要什么".- -...k -.-." 我得到了什么".- -... -.-.k"我相信那是因为我用 2 而不是 1 表示周期,但我真的不知道该怎么做。将不胜感激一些帮助此外,当用户写一个不可能的字符,如"*",我应该在那个位置放一个"?",我也在努力,我不知道我是否应该使用 if else 循环或什么

请帮助我,谢谢大家^^

是的,你是对的。这是因为您按顺序运行循环。

您需要对输入进行一次循环,然后检查一条输入是否是字母 - 从字母数组中获取翻译,如果它是来自莫尔斯数组的莫尔斯。

实际上,数组并不是最好的方法。使用一个地图要容易得多,其中字母将是一个键,MORSE 将是一个值。

那么代码可能如下所示:

 public static void main(String... args) {
    String[] letras = { "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" };
    String[] MORSE = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
            "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
    System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");

    Map<String, String> decodeMap = new HashMap<String, String>();
    for (int i=0; i< letras.length; i++)
    {
        decodeMap.put(letras[i], MORSE[i]);
    }
    Scanner in=new Scanner(System.in);
    String frase =in.nextLine();
    String resp = "";
    frase = frase.toLowerCase();
    String[] paraletras = frase.split(" ");     
    for (int i = 0; i < paraletras.length; i++) {
        boolean gotValue = false;
        for (Entry<String, String> decodeEntry: decodeMap.entrySet()) {             
          if (decodeEntry.getKey().equals(paraletras[i]))
          {
            // it is a letter print its MORSE
            resp += decodeEntry.getValue();
            gotValue = true;
            break;
          } else if (decodeEntry.getValue().equals(paraletras[i]))
          {             
            // it is a MORSE - print its letter
            resp += decodeEntry.getKey();
            gotValue = true;
            break;
          }
        } 
        if (gotValue)
        {
          resp += " ";
        }
    }
    System.out.print(resp);
}

你必须评估每个组或字符来猜测它是否是莫尔斯。

Example:
First group: 'a'
  is morse -> no
  is a letter -> yes -> then convert to morse
Second group: 'b' 
  is morse -> no
  is a letter -> yes -> then convert to morse
Third group: '-.-'
  is morse -> yes -> then convert to letter

这段代码可能更有效,但我认为它很容易理解。我希望这能帮助你提高你的编程技能。

import java.util.Scanner;
public class Morse2 {
    public static void main(String[] args) {
        String[] LETRAS = { 
                "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" };
        String[] MORSE = {
                ".-", "-...", "-.-.", "-..", ".", "..-.", 
                "--.", "....", "..", ".---", "-.-", ".-..", 
                "--", "-.", "---", ".--.", "--.-", ".-.", 
                "...", "-", "..-", "...-", ".--", "-..-", 
                "-.--", "--.." };
        System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
        Scanner in=new Scanner(System.in);
        String frase =in.nextLine();
        String resp = "";
        frase = frase.trim().toLowerCase();
        String[] chars = frase.split(" ");
        for (int i = 0; i < chars.length; i++) {
            String group = chars[i];
            String newChar = null;
            for (int j = 0; j < LETRAS.length; j++) {
                if (LETRAS[j].equals(group)) {
                    newChar =  MORSE[j];
                    break;
                }
            }   
            if (newChar == null) {
                for (int j = 0; j < MORSE.length; j++) {
                    if (MORSE[j].equals(group)) {
                        newChar =  LETRAS[j];
                        break;
                    }
                }
            }
            if (newChar == null) {
                System.out.println("Group not found: "+group);
                continue;
            }
            resp += newChar + " ";
        }
        System.out.print(resp);
        in.close();
    }
}

很抱歉成为这样的菜鸟,但可能也是因为我从 8 小时前就在这里,但为什么这段代码只给了我几次字母"a"的摩尔斯电码?

import java.util.Scanner;
public class Morse2 {
    public static void main(String[] args){
 String[] abecedario = { "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"};                  
 String[] MORSE = {
 ".-" ,"-...","-.-.","-.." ,"." , 
 "..-.","--." ,"....",".." ,".---", 
 "-.-" ,".-..","--" ,"-." ,"---" , 
 ".--.","--.-",".-." ,"..." ,"-" , 
 "..-" ,"...-",".--", "-..-","-.--", 
 "--.."}; 
System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
Scanner in=new Scanner(System.in);
String frase =in.nextLine();
String resp="";
frase=frase.toLowerCase();
String[] palavra=frase.split(" ");
for(int i=0;i< palavra.length;i++){
    String letra=palavra[i];
    String novochar="";
    for (int j=0;j<abecedario.length;j++){
        if (abecedario[j].equals(letra));
        novochar=MORSE[j];
        break;
    }
    if(novochar==""){
        for (int j=0;j<MORSE.length;j++){
        if (MORSE[j].equals(letra));
        novochar=abecedario[j];
        break; 
    }
    }
    if(novochar==""){
        novochar="?";
        continue;
    }   
    resp=resp+novochar+" ";
    }
    System.out.println(resp);
    }
}   

最新更新