试图从字符串数组中创建一个字符串
我将以一个事实说明这个问题,即我只有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无法找到符号分别。
我明白你是在Java类的介绍中,所以随时提问,但你1)不需要加入字符串和2)indexOf
真的是效率低下,你需要它的目的。 A HashMap<String, String>
是翻译者的完美对象。
我已经制作了这个双向映射类,它可以在两个字符串数组之间来回切换。将其保存到BiMap.java
文件中。
import java.util.HashMap;
public class BiMap {
private HashMap<String, String> forwardMap;
private HashMap<String, String> backwardMap;
public BiMap(String[] from, String[] to) {
forwardMap = new HashMap<>();
backwardMap = new HashMap<>();
for (int i = 0; i < from.length && i < to.length; i++) {
forwardMap.put(from[i], to[i]);
backwardMap.put(to[i], from[i]);
}
}
public String translateForward(String key) {
return forwardMap.get(key);
}
public String translateBackward(String key) {
return backwardMap.get(key);
}
}
下面是该类的示例用法。随意将您的用户提示添加回它。建议对类变量常量使用static final
修饰符。然后,我定义了一些private
方法来提取出翻译逻辑。
这是一个很好的习惯,在学习时尽可能多地编写可读和可重用的方法。
public class MorseEnglishTranslator {
private static final String[] ENGLISH = new String[]{
"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", " "};
private static final String[] MORSE = new String[]{
".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....",
"--...", "---..", "----.", "-----", "|"};
private static final BiMap MORSE_MAP = new BiMap(ENGLISH, MORSE);
private static final String DELIMITER = " ";
private static String toMorse(String s) {
StringBuilder sb = new StringBuilder();
String lower = s.toLowerCase();
for (int i = 0; i < lower.length(); i++) {
String c = String.valueOf(lower.charAt(i));
sb.append(MORSE_MAP.translateForward(c)).append(DELIMITER);
}
return sb.toString();
}
private static String fromMorse(String s) {
String[] split = s.split(DELIMITER);
StringBuilder sb = new StringBuilder();
for (String morse : split) {
sb.append(MORSE_MAP.translateBackward(morse));
}
return sb.toString();
}
public static void main(String[] args) {
String sentence = "Hello World";
String morse = toMorse(sentence);
String translated = fromMorse(morse);
System.out.println(sentence);
System.out.println(morse);
System.out.println(translated);
System.out.println(sentence.equalsIgnoreCase(translated));
}
}
采样运行
Hello World
.... . .-.. .-.. --- | .-- --- .-. .-.. -..
hello world
true
再一次,非常感谢你的帮助,但是就像我告诉东胜一样,我不得不使用数组,更不用说我们没有通过HashMap或者读取外部文件。 –
我没有使用数组,我只是使用数组创建了一个Hashmap。也根本不读取外部文件,只做了一个单独的课程。你已经使用过课程,即使你还没有学过他们的课程 –
对不起,我没有意识到,这只是我们没有理解任何一个概念,所以我会充满作弊。我们即将开始有多个班级。无论如何,这是非常好的,只是不是我可以包裹头脑的东西,很明显,我仍然有这些“低级”概念的问题。我会说,似乎试图使这个工作只使用数组和字符串似乎过于复杂,相比之下的替代品。 :) –
请注明您所使用的语言。 – sschale
对不起,Java 8 –
已经为她完成了,她只需要接受! – sschale