所以这是我的代码的一部分。如果我为 preTranslation 字符串输入“1111”,它应该打印出 true,但打印出 false。我猜 .equal() 无法比较这两个值,但我不知道另一种方式。有什么帮助吗?
import javax.swing.JOptionPane;
import java.util.Arrays;
public class test3
{
public static void main ( String [] args )
{
String s1 = "Morse";
// Decide whether Morse code or English
String decide = JOptionPane.showInputDialog("Enter 'English' for Morse to English code translation and 'Morse' for English to Morse code translation. Pay attention to Caps.");
// Enter String & decide whether to convert to Morse or English
String preTranslation = JOptionPane.showInputDialog("Enter the words you wish to translate.");
if ( decide.equals( s1 ))
toMorse( preTranslation );
else
toEnglish( preTranslation );
}
// Translate to Morse
public static void toMorse( String preTrans )
{
// Initialize english letters
char[] english = new char[3];
// Initialize english numbers
english[0] = 1;
english[1] = 2;
english[2] = 3;
// Initialize morse characters
String[] morse = {".","-", ".-"};
// Replace spaces with |
String phraseWithDelimiter = preTrans.replace( " ", "|");
String[] translation = new String[phraseWithDelimiter.length()];
System.out.println( phraseWithDelimiter.substring(0, 1).equals( english[0] )); // Should print out to be true
}
}