问题
this is my first post on this platform ,im kinda new to this java programing and also not that good at english :p
My teacher asked for a morse code translator that does morse to letters and vice versa
Here's the code i came up with:
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);}
}
The class compiles fine but im having some issues with my output,more specifically the order of the output:
e.g My input " a b -.- c " What i wanted ".- -... k -.-." What i got ".- -... -.-. k" I believe that's because i used 2 for cycles instead of 1 but i cant really tell how to do it.Would apreciate some help Also when the user writes an impossible character like "*" im suppossed to put an "?" in that position and im also strugling on that i dont know if i should use a if else cycle or what
Please help me and thank you everybody ^^
回答1:
Yes you are correct. It is because you run your loops sequentially.
You need one loop over your input, then check if one piece of input is a letter - take translation from letters array, if it is a MORSE take from Morse array.
Actaully arrays are not the best approach. It is much easier to use one Map where letter will be a key, MORSE will be a value.
then code may look like:
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);
}
回答2:
You have to evaluate every group or chars to guess if it is morse or not.
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
This code could be more efficient, but I think it is easy to understand. I hope this help you to improve your programming skills.
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();
}
}
回答3:
Sorry to be such a noob,but probably is also because ive been here since 8 hours ago but why is this code giving me only the morse code to the letter 'a' a couple times?
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);
}
}
来源:https://stackoverflow.com/questions/47359984/creating-a-morse-code-translator-for-my-programming-class-having-troubles-with-t