创建莫尔斯电码翻译为我的编程类,具有输出

问题描述:

这是我在这个平台上的第一篇文章的麻烦,即时通讯还挺新本的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);} 

    } 

类编译罚款,但有我的输出,更具体数量级的一些问题IM输出:

例如我的输入“a b -.- c” 我想要什么“.- -... k -.-。” 我得到了“.- -... -.-。k” 我相信那是因为我用了2个周期而不是1个,但我真的不知道该怎么做。想要一些帮助 当用户写入一个不可能的字符,如“*”即时消息,可以放一个“?”在这个位置和IM也strugling上,我不知道我是否应该使用的if else周期或什么

请帮助我,谢谢大家^^

+0

你好,Joao,你发布你的问题在葡萄牙的*?此外,我建议添加标签“爪哇”,所以人们可以帮助你更多... –

+0

奥基,谢谢你^^ –

是的,你是正确的。这是因为你依次运行你的循环。

你需要一个循环遍历你的输入,然后检查一个输入是否是一个字母 - 从字母数组中取出翻译,如果它是来自Morse数组的MORSE。

问题阵列不是最好的方法。使用一个字母将是一个关键字的地图要容易得多,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(); 
    } 

} 
+0

对不起,这样的小白,但可能是因为我一直在这里8小时前,但为什么是这段代码只给了我几次摩尔斯码给字母'a'? –

+0

此代码是确定的。你修改的代码有2个错误。看看循环内部的if语句。你已经关闭了if语句。由于这个原因,循环不能正常工作。 –

对不起,是这样的小白,但可能也正是因为我一直都在这里,因为13小时前,但为什么这个代码给我只有莫尔斯电码的字母“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); 
    } 
} 
+0

这是因为你没有在'j'的循环中做适当的'if'。看:'如果(abecedario [j] .equals(letra));' - 什么都不关闭。然后'novochar = abecedario [j];''总是'abecedario [0]'并且'for'立即崩溃。所以......你忘了这样做:'if(abecedario [j] .equals(letra))novochar = MORSE [j]; 休息; }' - 在第二个'for'循环中'if'相同 – Vadim