-1

我正在尝试做一个莫尔斯电码-英语双向翻译

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 可以完美翻译,但是 else 会导致错误。

另外,我试图做到这一点,所以如果我输入“this”,那么它将转到莫尔斯或英语翻译器,但它似乎不起作用

出于某种原因, else 一词会导致错误

4

3 回答 3

2

改变

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

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

这只是使您的两个 if 块都可以工作的解决方法……但是您编写逻辑的方式既不是 English->Morse 也不正确,也不是 Mosre->English 是正确的。

工作代码:

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();      
}

提示...

  1. 不要像以前那样使用过多的扫描仪……这一切都可以只用一台扫描仪来完成。
  2. 使用后总是关闭你的资源......这些是java中的好习惯......
于 2015-07-28T08:40:36.477 回答
1

a) ifs 后面的分号是错误的。

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

这意味着,不会执行任何操作。

b)您正在浏览英文字符,然后查看其中一个是否包含在输入中。走错路了。您必须通过输入并为此查找字符。你这样做的方式,“sos”将被编码为“oss”,因为它首先查看“o”,找到它,然后查看“s”,找到它,再次找到它 - 因为“o”首先出现在英文字母表中。

c) Morse -> 英语不能以这种方式工作,因为您无法查看单个莫尔斯字符。我建议通过“”(String.split)拆分输入,然后将结果与代码数组进行比较以找到正确的索引。目前,sos 将被检查为“.”、“.”、“.”、“-”、...、“.”。但是您要检查的是“...”、“---”、“...”,这将是"... --- ...".split(" ");

于 2015-07-28T08:42:34.183 回答
1

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;
            }
        }

    }
于 2015-07-28T08:46:04.203 回答