-1

我正在研究将英语转换为摩尔斯电码的翻译器,反之亦然。我编写了代码并且能够编译它,但是在输入要转换的句子后,命令提示符破折号只是转到下一行,什么也没有出现。我的代码有问题吗?

这是我的代码:(不是最有效的,因为我还是 java 新手,所以任何提示也将不胜感激)

import javax.swing.JOptionPane;

public class translator {
    public static void main ( String [] args ) {

        String s1 = "Morse";

        //Decide whether Morse code or English
        String decide = JOptionPane.showInputDialog("Enter 'English' for Morse to English code translation and 'Morse' for English to Morse code translation.Pay attention to Caps.");

        //Enter String
        String phrasep = JOptionPane.showInputDialog("Enter the words you wish to translate.");

        if ( decide.equals( s1 ))
            toMorse( phrasep );

        else
            toEnglish( phrasep );

    }

    // Translate to Morse
    public static void toMorse( String phrase1 )
    {
        char[] english = new char[36];
        char[] number = { 1 };

        for (  int i = 65, j = 0; i < 91; i++, j++)
        {
            english[j] = (char)i;
        }


        english[26] = 1;
        english[27] = 2;
        english[28] = 3;
        english[29] = 4;
        english[30] = 5;
        english[31] = 6;
        english[32] = 7;
        english[33] = 8;
        english[34] = 9;
        english[35] = 0;

        String[] morse = new String[36];

        morse[0] =  " .- ";
        morse[1] =  " -.. ";
        morse[2] =  " -.-. ";
        morse[3] =  " -.. ";
        morse[4] =  " . ";
        morse[5] =  " ..-. ";
        morse[6] =  " --. ";
        morse[7] =  " .... ";
        morse[8] =  " .. ";
        morse[9] =  " .--- ";
        morse[10] =  " -.- ";
        morse[11] =  " .-.. ";
        morse[12] =  " -- ";
        morse[13] =  " -." ;
        morse[14] =  " --- ";
        morse[15] =  " .--. ";
        morse[16] =  " --.- ";
        morse[17] =  " .-. ";
        morse[18] =  " ... ";
        morse[19] =  " - ";
        morse[20] =  " ..- ";
        morse[21] =  " ...- ";
        morse[22] =  " .-- ";
        morse[23] =  " -..- ";
        morse[24] =  " -.-- ";
        morse[25] =  " --.. ";
        morse[26] =  " .---- ";
        morse[27] =  " ..--- ";
        morse[28] =  " ...-- ";
        morse[29] =  " ....- ";
        morse[30] =  " ..... ";
        morse[31] =  " -.... ";
        morse[32] =  " --... ";
        morse[33] =  " ---.. ";
        morse[34] =  " ----. ";
        morse[35] =  " ----- ";

        String phrase = phrase1.replace( "//s+", "|");

        String[] translation = new String[phrase1.length()];

        for ( int j = 0, t = 0, n = 1; j < phrase.length(); j++)
        {
            while ( j < phrase.length() )
            {
                if ( phrase.substring(t, n ).equals ( english[j] ) )
                {
                    translation[t] = morse[j];
                    t++;
                    n++;
                }
            }
        }
    System.out.println( translation );
    }

    public static void toEnglish( String phrase)
    {
        System.out.println( phrase );
    }
}
4

1 回答 1

2

您的代码中有一个无限循环。

查看变量 J。您在 for/next 循环中设置变量,然后在 while 循环中测试变量。每次它完成 while 循环时,它都会测试是否 j <phrase.length()。但是由于 J 在您离开 while 循环之前不会改变,因此它永远不会离开。

你不需要这个测试两次。删除 while,让 for 循环处理它。

于 2012-08-16T00:22:00.847 回答