2

我一直在尝试创建一个摩尔斯电码翻译器,但遇到了一些问题。它可以编译,但是当我尝试运行它时,它只会询问第一个问题,即您要翻译哪种方式,而不是您要翻译什么。我知道我的第一个功能非常低效。我也使用Input.getString我认为不是标准的,但它基本上允许你输入一个字符串。

这是我的代码:

public class MorseCodeJavaProgram {
    public static void morse( String s3 ){
        int letters [ ] = new int [ 26 ];
        for ( int num = 0; num < s3.length(); num++ ){
                switch ( s3.charAt( num ) ){
                    case 'a':
                        System.out.print( ".- ");
                        break;
                    case 'b':
                        System.out.print( "-… ");
                        break;
                    case 'c':
                        System.out.print( "-.-. ");
                        break;
                    case 'd':
                        System.out.print( "-.. ");
                        break;
                    case 'e':
                        System.out.print( ". ");
                        break;
                    case 'f':
                        System.out.print( "..-. ");
                        break;
                    case 'g':
                        System.out.print( "--. ");
                        break;
                    case 'h':
                        System.out.print( "…. ");
                        break;
                    case 'i':
                        System.out.print( ".. ");
                        break;
                    case 'j':
                        System.out.print( ".--- ");
                        break;
                    case 'k':
                        System.out.print( "-.- ");
                        break;
                    case 'l':
                        System.out.print( ".-.. ");
                        break;
                    case 'm':
                        System.out.print( "-- ");
                        break;
                    case 'n':
                        System.out.print( "-. ");
                        break;
                    case 'o':
                        System.out.print( "--- ");
                        break;
                    case 'p':
                        System.out.print( ".--. ");
                        break;
                    case 'q':
                        System.out.print( "--.- ");
                        break;  
                    case 'r':
                        System.out.print( ".-. ");
                        break;  
                    case 's':
                        System.out.print( "... ");
                        break;
                    case 't':
                        System.out.print( "- ");
                        break;  
                    case 'u':
                        System.out.print( "..- ");
                        break;  
                    case 'v':
                        System.out.print( "...- ");
                        break;
                    case 'w':
                        System.out.print( ".-- ");
                        break;
                    case 'x':
                        System.out.print( "-..- ");
                        break;
                    case 'y':
                        System.out.print( "-.-- ");
                        break;
                    case 'z':
                        System.out.print( "--.. ");
                        break;
                    case ' ':
                        System.out.print( " | ");
                        break;
                }

            }
    }
    public static void toEnglish( String s1 ){
    String english [ ] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", " " };
    String morse [ ] = { ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", "…. ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "| " };

        for ( int num = 0; num < s1.length(); num++ ){
            if ( s1.charAt ( num ) == ' '){
                for ( int num2 = num; num2 < s1.length(); num2++ ){
                    if ( s1.charAt ( num2++ ) == ' '){
                        for ( int num3 = 0; num < 26; num3++ ){
                            if ( s1.substring( num++, num2 + 2 ) == ( morse [ num3 ] )){
                                System.out.print( english [ num3 ] );
                            }
                        }
                    }

                }


            }
        }
    }

    public static void main( String [] args ){
        String s2 = Input.getString( "To Morse or From Morse" );
        if ( s2 == "From Morse" ){
            String s1 = Input.getString( "Please type a phrase in English" );
            toEnglish( " " + s1 + " " );
        }
        if ( s2 == "To Morse" ){
            String s3 = Input.getString( "Please type a phrase in Morse Code" );
            morse( s3 );
        }
    }
    }
4

2 回答 2

4

您正在使用 比较字符串==。在 Java 中,==比较引用,而不是内容

将它们更改为 use .equals(),如下所示:

if ( s2 == "From Morse" ){

应该:

if ( s2.equals( "From Morse" ) ){

当然,这也应该适用于您的其他字符串比较。(您的char比较照原样很好。)

于 2012-10-21T03:55:34.130 回答
1

您正在将字符串值与 == 运算符进行比较。但是 == 运算符只检查对象引用是否相等。

 if ( s2 == "From Morse" ){
            String s1 = Input.getString( "Please type a phrase in English" );
            toEnglish( " " + s1 + " " );
        }

相反,您尝试

 if ("From Morse".equals(s2) ){
                String s1 = Input.getString( "Please type a phrase in English" );
                toEnglish( " " + s1 + " " );
于 2012-10-21T04:00:42.647 回答