Java 编译测试并运行?有人能给点建议吗?

Java 编译测试并运行?有人能给点建议吗?,java,testing,compilation,runtime-error,Java,Testing,Compilation,Runtime Error,此Java代码应提示用户在摩尔斯电码和英语之间进行选择,然后提示他们以所选语言输入字符串。然后,它应该生成另一种语言的翻译。它在我的机器上编译,尽管有时不能正常工作 其他人能试着帮我运行一下,然后告诉我它是否有效吗?如果不是,您能指出代码中产生运行时错误的错误吗 public class MorseCodeJavaProgram { public static void morse( String s3 ) { int letters [ ] = new int

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

其他人能试着帮我运行一下,然后告诉我它是否有效吗?如果不是,您能指出代码中产生运行时错误的错误吗

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

嗯……当我测试代码时,在尝试将摩尔斯电码翻译成英语时,它实际上在toEnglish()方法中包含了一个错误,但是,这肯定不是你得到的错误,我显然只在尝试将摩尔斯电码翻译成英语时得到了一个例外

我得到一个StringIndexOutOfBoundsException异常,引发异常的行是:

if ( s1.substring( num++, num2 + 2 ) == ( morse [ num3 ] ))
首先,您缺少english[]字符串数组中的一个元素。请注意,该数组中缺少“w”。如果缺少一个元素,则english[]数组和morse[]数组之间的索引未命中匹配。我们需要确保“w”包含在english[]字符串数组中,否则当遇到“w”时,代码将无法翻译它

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

if (s1.substring(num++, num2 + 2).equals(morse[num3]))
大部分编码错误都是由于丢失了equals字符造成的,即使编译很好。使用==还存在其他问题,最好涵盖这些问题:

这并不是说这可以修复toEnglish()方法代码块中的错误。老实说,这段代码让我很头疼,更不用说对它的逻辑进行排序了。这里没有不尊重的意思@Skier1999,这是一个非常好的尝试。这段代码的问题在于,您在for/loops和条件if语句中增加了索引,这将使您的索引变得异常,最终,StringIndexOutOfBounds异常

与其在toEnglish()方法中修复此逻辑(只需稍加调整即可),我实际上想提出一种不同的翻译方法。实际上,两种翻译都有。让我们从两个字符串数组开始,english[]morse[]。我们把它们去掉,创建一个二维数组,它将包含字母数字字符及其相关的摩尔斯电码等价物。可以说是一种翻译表。然后让我们将这个2D数组(名为morseEnglish)放在类构造函数下,以便类中的所有方法都可以访问它。以下是我提议的阵列:

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()方法开始。让我们摆脱长开关/案例,只需使用两个for/循环即可。让我们忘记使用charAt()方法跳过输入文本字符串的每个字符,而是使用string.substring()方法。然后,我们将遍历我们的morseEnglish2D数组,看看是否可以为每个字符找到匹配项,当我们进行匹配时,该字符的相关Morse元素将附加到一个名为translation的字符串中。以下是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);
}
publicstaticvoidtomorse(字符串s3){
字符串翻译=”;
对于(int i=0;i
这里…应该包括文本字符串到摩尔斯电码的翻译(转换)。现在,让我们来处理导致异常的toEnglish()方法。因为我们有我们的2D数组转换表,我们基本上可以做同样的事情将摩尔斯电码字符串转换为英语(字母数字)文本。我们只是在成功比较的morseEnglish2D数组中使用不同的索引,以获得与Morse数据相关的字母数字。因为每个莫尔斯电码字符序列都由空格分隔,所以我们可以使用String.split()方法将整个莫尔斯电码字符串放入另一个名为Code[]的字符串数组中(此处没有charAt()方法)。通过这种方式,我们可以简单地迭代新的代码[]数组,获得完整的莫尔斯字符表示,并将其与我们的2D翻译表数组(morseEnglish[])进行比较,然后拉出相关的字符元素。以下是建议的英语学习方法()

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);
}
publicstaticvoid-toEnglish(字符串s1){
字符串代码[]=s1.split(“”);
字符串翻译=”;
for(int i=0;i
这要容易得多
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);
}
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()); }
            }
        }
    }
}