抱歉,这似乎是一个愚蠢的问题,但我无法弄清楚如何将 Java 中用户输入的子字符串与空白进行比较。在我的程序中,我使用
Input.getString()
这不常见,但 Input.class 用于接收用户的输入。这是我的程序:
public class Project1
{
static String E [] = { "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " " };
static String M [] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "|" };
public String input[];
public String output[];
static String TranslationType;
static boolean Bool;
static String TextToTranslate;
static int i;
static int SectionOfString;
static StringBuffer emcStringBuffer = new StringBuffer();
static StringBuffer mceStringBuffer = new StringBuffer();
static String emc = "English-MorseCode";
static String mce = "MorseCode-English";
static StringBuffer MorseCodeLetter = new StringBuffer();
static String emcString;
static String mceString;
static String space = " ";
public static void main( String [] args )
{
if ( E.length != M.length )
{
System.out.println( "Error" );
}
TranslationType = Input.getString( "Please choose a method of translation (\"English-MorseCode\" or \"MorseCode-English\") " );
if ( TranslationType.equals( "English-MorseCode" ) )
{
Bool = true;
System.out.println( TranslationType );
}
else if( TranslationType.equals( "MorseCode-English" ) )
{
Bool = false;
System.out.println( TranslationType );
}
while ( !TranslationType.equals( emc ) && !TranslationType.equals( mce ) )
{
System.out.println( "Error: \"" + TranslationType + "\" is not a valid response.");
TranslationType = Input.getString( "Please choose a method of translation (\"English-MorseCode\" or \"MorseCode-English\") " );
if ( TranslationType.equals( "English-MorseCode" ) )
{
Bool = true;
System.out.println( TranslationType );
}
else if( TranslationType.equals( "MorseCode-English" ) )
{
Bool = false;
System.out.println( TranslationType );
}
}
TextToTranslate = Input.getString( "What would you like to translate?" );
TextToTranslate = TextToTranslate.toUpperCase();
System.out.println( TextToTranslate );
if ( Bool == true )
{
for ( SectionOfString = 0; SectionOfString < TextToTranslate.length(); SectionOfString++ )
{
for ( i = 0; i < E.length; i++ )
{
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( E[i] ) )
{
emcStringBuffer.append( M[i] + " " );
}
}
}
emcString = emcStringBuffer.toString();
System.out.println( emcString );
}
else if ( Bool == false )
{
for ( SectionOfString = 0; SectionOfString < TextToTranslate.length(); SectionOfString++ )
{
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( space ) )
{
for ( i = 0; i < M.length; i++ )
{
if ( MorseCodeLetter.equals( M[i] ) )
{
mceStringBuffer.append( E[i] );
System.out.println( MorseCodeLetter + ";" + mceStringBuffer );
}
}
}
else
{
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( M[36] ) )
{
mceStringBuffer.append( E[36] );
}
else
{
MorseCodeLetter.append( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ) );
}
}
}
mceString = mceStringBuffer.toString();
System.out.println( mceString );
}
System.out.println( "Done" );
}
}
问题似乎是这部分代码在将摩尔斯电码翻译成英文时从未使用过:
if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( space ) )
{
for ( i = 0; i < M.length; i++ )
{
if ( MorseCodeLetter.equals( M[i] ) )
{
mceStringBuffer.append( E[i] );
System.out.println( MorseCodeLetter + ";" + mceStringBuffer );
}
}
}
这导致莫尔斯电码字母不被分开。例如,如果 TextToTranslate 的输入是
. .-. | .-. .
那么程序会认为..-..-. 是一个词。我不确定这是否是翻译结果未打印到终端(或 windows 上的命令提示符)的原因。
另外,我之前尝试让空间成为静态字符,我只是尝试将空间作为最终字符,在 if 构造中使用以下内容,我遇到了同样的问题:
TextToTranslate.charAt( SectionOfString ) == space
我不确定为什么会出现这个问题,我试图找到一个类似的问题但无法找到,所以如果有人知道如何解决这个问题,我将不胜感激。
注意:我在 Java 编程论坛上问了同样的问题(用户:Indybones33)