嗨,我正在尝试制作一个将英语翻译成莫尔斯语的 java 程序,反之亦然。基本上我很难让所有东西变得兼容..以及如何使用replace all方法来让所有输入的变量被对应的i Morse索引替换。我并不想走这么多捷径……我真的很努力,想完成这件事。太感谢了!
import java.util.Scanner;
public class ProjMorse
{
public static void main( String [] args )
{
Scanner input = new Scanner(System.in);
String [] alpha = {"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"," "};
String [] dottie = {".-", "-...", "-.-.", "-..", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "|"};
System.out.println("To convert from English to Morse enter English");
String ans = input.nextLine();
if(ans.equals("English"))
{
System.out.println( "Please enter the text you would like to convert to Morse Code: ");
String English = input.nextLine();
char[] translates = (English.toLowerCase()).toCharArray();
for (int i = 0; i < alpha.length; i++)
{
String s = translates[i].replaceAll('i', (dottie[i]));
}
String s = new String(dottie[i]);
System.out.println(s);
}
else
{
System.out.println( "Please enter the text you would like to convert to English (separate words with '|'):");
String code = input.nextLine();
String[] translates = code.split("[|]", 0);
for (int j = 0; j < dottie.length; j++)
{
alpha[j] = String.valueOf(translates[j]);
}
String s = new String(alpha[j]);
System.out.println(s);
}
}
}