当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Morse code - English - translator

问题描述:

I'm trying to do a morse code-English both ways translator

import java.util.Scanner;

public class JavaProject {

public static void main(String[] args) {

// TODO Auto-generated method stub

char [] Eng = { '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 [] Code = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };

System.out.println(" Enter Morse Code or something in English. A for english, or B for morse code.");

Scanner a = new Scanner(System.in);

String z = a.nextLine();

if (z.equals("A"));

{

System.out.println("enter in english");

Scanner f = new Scanner(System.in);

String b = f.nextLine();

for ( int x = 0; x < Eng.length; x++ )

{

for ( int y = 0; y < b.length (); y++ )

{

if ( Eng [ x ] == b.charAt ( y ) )

System.out.print ( Code [ x ] + " " );

}

}

}

else if (z.equals("B"));

{

System.out.println("enter in Morse");

Scanner v = new Scanner(System.in);

String n = v.nextLine();

for ( int i = 0; i < Code.length; i++ )

{

for ( int p = 0; p < n.length (); p++ )

{

if ( Eng [ i ] == n.charAt ( p ) )

System.out.print ( Eng [ i ] + " " );

}

}

}

}

}

English to morse translates perfectly, but the word else causes an error.

Also, I tried to make it so if I type "this" then it will go to either a morse or English translator, but it doesn't seem to work

for some reason, the word else causes an error

网友答案:

Change

if (z.equals("B"));
if (z.equals("A"));

to

if (z.equals("B"))
if (z.equals("A"))

This is just a fix for making your both if blocks working... But the way you have written your logic neither English->Morse is right nor Mosre->English is right.

Working Code:

public static void main(String[] args) {

char[] Eng = { '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[] Code = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
                "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
                "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
                "-.--", "--..", "|" };
System.out.println("Enter your choice: \n1. A for English -> Morse Code \n2. B for Morse Code -> English");
Scanner a = new Scanner(System.in);
String z = a.nextLine();
if (z.equalsIgnoreCase("A")) {
System.out.println("enter in english");         
String b = a.nextLine();
for (int x = 0; x < b.length(); x++) {
    for (int y = 0; y < Eng.length; y++) {
        if (Eng[y] == b.charAt(x))
            System.out.print(Code[y] + " ");
        }
    }    
} 
else if (z.equalsIgnoreCase("B")) {
    System.out.println("enter in Morse");           
    String n =a.nextLine();
    String[] arr = n.split(" ");
    for (int p = 0; p < arr.length; p++) {
        for (int y = 0; y < Code.length; y++) {
            if (Code[y].equals(arr[p]))
                System.out.print(Eng[y]);
            }    
        }    
    }
a.close();      
}

Tips...

  1. Don't use too many scanners as you did... It can all be done with one scanner only.
  2. Always close your resources after use... These are good practices in java...
网友答案:

a) The semicolons after the ifs are wrong.

if (z.equals("A"));

This means, nothing will be executed.

b) You are going through the english characters and then look if one of them is contained in the input. Wrong way around. You have to go through the input and look up the characters for that. The way you are doing it, "sos" will be coded as "oss", because it first looks at "o", finds it, then looks at "s", finds it, finds it again - because "o" comes first in the english alphabet.

c) Morse -> English cannot work this way, since you cannot look at single Morse characters. I would suggest splitting the input by " " (String.split) and then compare the result with the Code array to find the correct index. At the moment, sos would be checked as ".", ".", ".", "-", ... ,".". But what you want to check is "...", "---", "...", which would be the result of "... --- ...".split(" ");

网友答案:

The problem is that you only test one char at the morsecode in this snippet:

for ( int p = 0; p < n.length (); p++ )
        {
            if ( Eng [ i ] == n.charAt ( p ) )

            System.out.print ( Eng [ i ] + " " );
        }

To identify the whole term try to use:

n.compareTo(Code[i]) == 0)

final code:

    public static void main(String[] args) {

        char[] Eng = { '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[] Code = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
                "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
                "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
                "-.--", "--..", "|" };
        System.out
                .println(" Enter Morse Code or something in English. A for english, or B for morse code.");
        Scanner a = new Scanner(System.in);
        String z = a.nextLine();
        if (z.equals("A")) {
            System.out.println("enter in english");
            Scanner f = new Scanner(System.in);
            String b = f.nextLine();
            for (int x = 0; x < Eng.length; x++) {
                for (int y = 0; y < b.length(); y++) {
                    if (Eng[x] == b.charAt(y))

                        System.out.print(Code[x] + " ");
                }
            }

        } else if (z.equals("B"))

    {
        System.out.println("enter in Morse");
        Scanner v = new Scanner(System.in);
        String n = v.nextLine();

        for (int p = 0; p < Code.length; p++) {
            if (n.compareTo(Code[p])==0){

                System.out.print(Eng[p] + " ");
                break;
            }
        }

    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: