我將以一個事實說明這個問題,即我只有1個月的時間學習編程,而這個學校任務讓我難住了。具體來說,它的摩爾斯電碼英語翻譯(反之亦然)......這裏是我堅持的部分上:試圖從字符串數組中創建一個字符串
/*
* A program that asks the user to select which they would like to do: translate
* from English to Morse code, or Morse code to English. The list of characters
* for each language is stored using arrays. The program will then perform and return
* the translations.
*/
import java.util.Scanner;
public class MorseEnglishTranslator
{
public static void main(String [] args)
{
int translateChoice = 0; // Variable for person's choice for direction of translation
Scanner inputText = new Scanner(System.in); // Create a Scanner to obtain user input
System.out.print("Enter 1 to translate from English to Morse code, 2 for Morse code to English: ");
translateChoice = inputText.nextInt();
if (translateChoice == 1);
{
System.out.print("Enter a letter, word, or phrase you would like translated: ");
}
if (translateChoice == 2);
{
System.out.print("Enter the Morse code you would like translated, separate letters and words with a |: ");
}
String userStr = inputText.nextLine();
translator(translateChoice, userStr);
} // Closes main
public static void translator(int translateChoice, String userStr) // Method for translating either direction
{
String english [] = {"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 s1 = String.join(" ", english); // Assigns the contents of english array to one string
String morse [] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..",
"----.", "-----", "|"};
String s2 = String.join("|", morse); // Assigns the contents of morse array to one searchable string
if (translateChoice == 1);
{
String userStrLower = userStr.toLowerCase(); // In case user capitalized anything, changes everything to lowercase
for (int allFound=0; allFound <userStrLower.length(); allFound ++)
{
allFound = s1.indexOf(userStrLower); // Variable 'allFound' to receive the search of s1 using userStrLower
System.out.print(allFound);
}
}
if (translateChoice == 2);
{
for (int allFound=0; allFound <userStr.length(); allFound ++)
{
allFound = s2.indexOf(userStr); // Variable 'allFound' to receive the search of s2 using userStr
System.out.print(allFound);
}
}
} // Closes translator
} // Closes class
字符串S1和S2的兩個選擇我用考慮,但S1版在New之後告訴我有一個預期的分號。連接選項,我試圖作爲String.join與連接,說沒有合適的構造函數vs無法找到符號分別。
請註明您所使用的語言。 – sschale
對不起,Java 8 –
已經爲她完成了,她只需要接受! – sschale