此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 );
}
}
}
当我收到错误时,它会指出&#34;检查控制台是否有可能的错误消息,无法启动。&#34;
下面我添加了要编译为Input.class
的Input.java文件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;
}
}
答案 0 :(得分:0)
嗯....当我测试代码时,它确实在尝试将莫尔斯代码翻译成英语时在 toEnglish()方法中包含错误但是,它肯定不是你得到的错误,我显然只是在尝试将摩尔斯电码翻译成英文时才会遇到异常。
我收到 StringIndexOutOfBoundsException 异常,抛出异常的行是:
if ( s1.substring( num++, num2 + 2 ) == ( morse [ num3 ] ))
对于初学者,您缺少 english [] 字符串数组中的元素。请注意,您错过了&#34; w&#34;在那个数组中。如果缺少一个元素,您现在在 english [] 数组和 morse [] 数组之间存在索引未匹配。我们需要确保&#34; w&#34;包含在 english [] 字符串数组中,否则当&#39; w&#39;遇到的代码无法翻译它。
另外,如果可能的话,在比较字符串时,尽量避免在条件语句中使用==(double equals)。请改为使用 String .equals()方法,如下所示:
if (s1.substring(num++, num2 + 2).equals(morse[num3]))
编码错误的公平份额是由于错过了等于字符,即使编译很好。使用==还有其他问题,它们最好涵盖here:
这并不能解决您在 toEnglish()方法代码块中的错误。说实话,这段代码让我头疼只是为了看看它,更不用说对它的逻辑进行排序了。这里没有任何不尊重@ Skier1999,这是一个非常好的尝试。您对此代码的问题在于,您在for / loops和条件if语句中递增索引,这会使您的索引摆脱wack,然后最终...... StringIndexOutOfBounds 例外。
而不是在 toEnglish()方法中修复这个逻辑,这可以通过一些调整来完成,我想实际提出一种不同的方法来进行这种翻译。好吧,两个翻译实际上。让我们从两个String Arrays开始, 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 / loop代替。让我们忘记用 charAt()方法跳过输入文本字符串的每个字符,而是使用 String .substring()< / strong>方法。然后我们将遍历我们的 morseEnglish 2D数组,看看我们是否可以为每个字符匹配,当我们将相关的莫尔斯元素添加到该字符时,会附加到一个方便地命名为 翻译 即可。以下是 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()方法。因为我们有2D数组转换表,所以我们基本上可以做同样的事情来将莫尔斯代码字符串转换为英文(字母数字)文本。我们只是在 morseEnglish 2D数组中使用不同的索引进行成功比较,以获得相关的字母数字到莫尔斯数据。因为每个摩尔斯电码代码字符序列都由空格分隔,所以我们可以使用 String .split()方法将整个摩尔斯电码代码字符串放入另一个名为 code [] (没有 charAt()方法)。这样我们就可以简单地遍历新的 code [] 数组,获得完整的莫尔斯字符表示并将其与我们的2D翻译表数组进行比较( 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,但我认为现在这是你最容易理解的。
我也在你的代码中调整了一些其他的东西。 输入类似乎运行正常,但对于您目前使用这段代码所做的事情有点过分,更不用说该类相对于 JOptionPane.showInputDialog()相对有限了可以真正做到但是什么......它的功能性。这是整个 MorseCodeJavaProgram类:
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()); }
}
}
}
}
希望这有点帮助。