【发布时间】:2016-01-02 20:55:42
【问题描述】:
此 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 );
}
}
}
当我收到错误消息时,它会显示“检查控制台是否有可能的错误消息,无法启动。”
下面我添加了要编译的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;
}
}
【问题讨论】:
-
将您遇到的错误添加到您的问题中
-
@HocineDJEMAI 我刚刚编辑了问题以包含错误消息
-
@Rakim 以上是 Input.Class 和你的想法可能真的可以帮助我。
-
“检查控制台是否有可能的错误消息,无法启动。”本身不是错误消息。要期待答案,您必须付出最少的努力
-
@HocineDJEMAI 我已经编译并运行了程序。它运行。我输入了所有信息,但没有打印任何内容,而是弹出错误消息(带有 ! 的黄色三角形和上面的 java 咖啡杯)这正是我感到困惑的原因。我以前从未见过这种情况,并且是编码新手。请帮我。我不知道还能说什么,因为在研究之后我不明白这个错误,所以我想问其他可能的人。
标签: java testing compilation runtime-error