编译测试和运行?有人可以提供任何建议吗?

Compile test and run? Can someone give any advice?

此 Java 代码应提示用户在摩尔斯电码和英语之间进行选择,然后提示他们以所选语言输入字符串。然后它应该产生另一种语言的翻译。它在我的机器上编译,虽然它有时不能正常工作。

其他人可以为我尝试 运行 它并告诉我它是否有效吗?如果不是,您能否指出我的代码中产生 运行time 错误的错误?

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)
    {
        System.out.println("Begin Program");

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

        if ("To Morse".equals(s2) )
        {
            String s3 = Input.getString( "Please type a phrase in Morse Code" );
            morse( s3 );
        }
    }
}

当我收到错误消息时,它显示 "Check console for possible error message, unable to be launched."

下面我把要编译的Input.java文件加到Input.class

import javax.swing.*;


public class Input
{
    public static byte getByte( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Byte.parseByte( input );
    }

    public static short getShort( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Short.parseShort( input );
    }

    public static int getInt( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Integer.parseInt( input );
    }

    public static long getLong( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Long.parseLong( input );
    }

    public static float getFloat( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Float.parseFloat( input );
    }

    public static double getDouble( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Double.parseDouble( input );
    }

    public static boolean getBoolean( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return Boolean.parseBoolean( input );
    }

    public static char getChar( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return input.charAt(0);
    }

    public static String getString( String s )
    {
        String input = JOptionPane.showInputDialog( s );
        return input;
    }

}

嗯....当我测试代码时,它确实在 toEnglish() 方法中包含一个错误,当试图将摩尔斯电码翻译成英语时,这绝对是不是你得到的错误,我显然只在尝试将摩尔斯电码翻译成英语时得到例外。

我得到一个 StringIndexOutOfBoundsException 异常,抛出异常的行是:

if ( s1.substring( num++, num2 + 2 ) == ( morse [ num3 ] ))

对于初学者,您在 english[] 字符串数组中缺少一个元素。请注意,您缺少该数组中的 "w"。由于缺少一个元素,您现在在 english[] 数组和 morse[] 数组之间有一个索引 miss-match。我们需要确保 "w" 包含在 english[] 字符串数组中,否则当遇到 'w' 时,代码将无法翻译它。

另一件事,如果可能的话,在比较字符串时尽量避免在条件语句中使用 ==(双等号)。进入使用 String.equals() 方法的思路,如下所示:

if (s1.substring(num++, num2 + 2).equals(morse[num3]))

尽管编译没问题,但仍有相当一部分编码错误是由于缺少等号字符造成的。使用 == 还存在其他问题,最好涵盖这些问题 here

请注意,这并不能解决 toEnglish() 方法代码块中的错误。老实说,这段代码光是看就头疼,更别说梳理其中的逻辑了。 @Skier1999 没有不尊重的意思,这是一个非常好的尝试。这段代码的问题在于,您在 for/loops 和条件 if 语句中增加了索引,这使您的索引变得异常,然后最终.... StringIndexOutOfBounds 异常。

我不想在 toEnglish() 方法中修复此逻辑,只需稍加调整即可完成,我实际上想提出一种不同的方法来进行此翻译。好吧,实际上这两种翻译。让我们从两个字符串数组开始,english[]morse[]。我们如何废弃这些并创建一个二维数组,其中将包含字母数字字符及其相关的摩尔斯电码等价物。可以说是一种翻译table。然后让我们将这个二维数组(名为 morseEnglish)放在 class 构造函数下,以便 class 中的所有方法都可以访问它。这是我建议的数组:

static String[][] morseEnglish = {{"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","--.."}, {" ","|"}, {".",".-.-.-"}, {",","--..--"},
        {":","---..."}, {"?","..--.."}, {"'",".----."}, {"-", "-....-"}, {"/","-..-."}, 
        {"\"",".-..-."}, {"@",".--.-."}, {"=","-...-"}, {"(","-.--.-"}, {")","-.--.-"}, 
        {"0","-----"}, {"1",".----"}, {"2","..---"}, {"3","...--"}, {"4","....-"}, {"5","....."},
        {"6","-...."}, {"7","--..."}, {"8","---.."}, {"9","----."}};

这个数组还包括莫尔斯码的数字和标点符号。

现在让我们从 toMorse() 方法开始。让我们摆脱那些冗长的 switch/case 事情,只用一对 for/loops 代替。让我们忘记使用 charAt() 方法跳过输入文本字符串的每个字符,而是使用 String。 substring() 方法。然后我们将遍历我们的 morseEnglish 二维数组,看看我们是否可以为每个字符找到一个匹配项,当我们执行相关的摩尔斯元素时,该字符被附加到一个方便命名的字符串 翻译。下面是 toMorse() 方法的样子:

public static void toMorse(String s3) {
    String translation = "";
    for ( int i = 0; i < s3.length(); i++ ) {
        String c = s3.substring(i, i+1);
        for (int j = 0; j < morseEnglish.length; j++){
            if (morseEnglish[j][0].equals(c)) {
                if (translation.equals("")) { translation = morseEnglish[j][1]; }
                else { translation+= " " + morseEnglish[j][1]; }
            }
        }
    }

    System.out.println("Morse Code:  " + translation);
    JOptionPane.showMessageDialog(null, translation, "English To Morse Code", JOptionPane.INFORMATION_MESSAGE);
}

那里...应该涵盖文本字符串到摩尔斯电码的翻译(转换)。现在让我们处理无论如何都会导致异常的 toEnglish() 方法。因为我们有二维数组转换 table,我们基本上可以做同样的事情来将摩尔斯电码字符串转换为英语(字母数字)文本。我们只是在我们的 morseEnglish 二维数组中使用不同的索引来成功比较以获得相关的字母数字到莫尔斯数据。因为每个摩尔斯电码字符序列由空格分隔,我们可以使用 String.split() 方法将整个摩尔斯电码字符串放入 yet另一个名为 code[] 的字符串数组(这里没有 charAt() 方法)。这样我们就可以简单地遍历新的 code[] 数组,获得完整的摩尔斯字符表示并将其与我们的二维翻译 table 数组(morseEnglish [][]) 然后拉出相关的字符元素。这是建议的 toEnglish() 方法:

public static void toEnglish(String s1) {
    String code[] = s1.split(" ");
    String translation = "";
    for (int i = 0; i < code.length; i++) {
        for (int j = 0; j < morseEnglish.length; j++){
            if (morseEnglish[j][1].equals(code[i])) {
                translation+= morseEnglish[j][0];
            }
        }
    }

    System.out.println("English:  " + translation);
    JOptionPane.showMessageDialog(null, translation, "Morse Code To English", JOptionPane.INFORMATION_MESSAGE);
}

在我看来,这更容易理解,也更有效。还有其他方法可以执行此类操作,例如使用 Map,但我认为目前这是您最容易掌握的方法。

我还在您的代码中调整了一些其他内容。 输入 Class 似乎工作正常,但对于您当前使用这段代码所做的事情来说有点矫枉过正,更不用说 class 是相对有限的JOptionPane.showInputDialog() 真正可以做什么,但到底是什么......它是功能性的。这是整个 MorseCodeJavaProgram Class:

package morsecodejavaprogram;

import javax.swing.JOptionPane;

public class MorseCodeJavaProgram {
    static String[][] morseEnglish = {{"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","--.."}, {" ","|"}, {".",".-.-.-"}, {",","--..--"},
            {":","---..."}, {"?","..--.."}, {"'",".----."}, {"-", "-....-"}, {"/","-..-."}, 
            {"\"",".-..-."}, {"@",".--.-."}, {"=","-...-"}, {"(","-.--.-"}, {")","-.--.-"}, 
            {"0","-----"}, {"1",".----"}, {"2","..---"}, {"3","...--"}, {"4","....-"}, {"5","....."},
            {"6","-...."}, {"7","--..."}, {"8","---.."}, {"9","----."}};

    public static void toMorse( String s3 ) {
        String translation = "";
        for ( int i = 0; i < s3.length(); i++ ) {
            String c = s3.substring(i, i+1);
            for (int j = 0; j < morseEnglish.length; j++){
                if (morseEnglish[j][0].equals(c)) {
                        if (translation.equals("")) { translation = morseEnglish[j][1]; }
                        else { translation+= " " + morseEnglish[j][1]; }
                }
            }
        }
        System.out.println("Morse Code:  " + translation);
        JOptionPane.showMessageDialog(null, translation, "English To Morse Code", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void toEnglish( String s1 ) {
        String code[] = s1.split(" ");
        String translation = "";
        for (int i = 0; i < code.length; i++) {
            for (int j = 0; j < morseEnglish.length; j++){
                if (morseEnglish[j][1].equals(code[i])) {
                    translation+= morseEnglish[j][0];
                }
            }
        }
        System.out.println("English:  " + translation);
        JOptionPane.showMessageDialog(null, translation, "Morse Code To English", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main( String [] args) {
        JOptionPane.showMessageDialog(null, "Select OK To Begin Translating:", "Morse Code/English Translator", JOptionPane.INFORMATION_MESSAGE);
        System.out.println("Begin Program");
        String s2 = "";
        while (!s2.toLowerCase().equals("quit")) {
            s2 = UserInput.getString( "Enter either 'To Morse' or 'To English'.\n"
                    + "To Quit enter the word 'Quit':\n(not case sensitive)\n" );
            if (s2 == null) { break; }
            if ("to morse".equals(s2.toLowerCase())  ) {
                String s3 = UserInput.getString( "Please type a phrase in English" );
                if (s3 != null) { toMorse(s3.toLowerCase()); }
            }
            if ("to english".equals(s2.toLowerCase()) ) {
                String s1 = UserInput.getString( "Please type a phrase in Morse Code" );
                if (s1 != null) { toEnglish(s1.toLowerCase()); }
            }
        }
    }
}

希望对您有所帮助。