我正在做一个翻译,把英语翻译成一种叫Talhamr的合成语言。要做到这一点,我将用户输入并使其成为数组。然后,我对一个包含英语单词的字库数组进行测试,其中相应的Talhamr单词就在它旁边。如果英语单词有效,我想用相应的Talhamr词代替它。有人能帮我把这个编码吗?贝娄是我到目前为止的密码。
主修班:
import java.util.Scanner;
public class Translator {
public static String userInput = "";
public static String[] wordBank = {"a/an","Ai","about","circa" /*...*/};
public static void main(String[] args) {
/**
* getting input
*/
Scanner scanner = new Scanner(System.in);
System.out.print("Write something to be translated into ancient language: ");
/**
* formatting input
*/
userInput.toLowerCase();
userInput = scanner.nextLine();
String[] wordArray = userInput.split(" ");
/**
* Creating a, and preforming english to talhammir, translation
*/
EtoT e2t = new EtoT();
e2t.translateToTalhamr(wordArray, wordBank);
System.out.println(wordArray[0]);
/**
* This is how I formatted the word bank. After I went to quizlet and changed all the spaces between rows and columns to "-"
* String makeNice = ...;
* System.out.println("\""+ makeNice.replace("-", "\",\""));
*/
}//end of main method
}//end of Translator class第二类包含翻译方法:
public class EtoT {
/**
* establishing word bank to loop through and translate
*/
public String[] wordBank = {"a/an","Ai","about","circa" /*...*/};
public EtoT() {
}
public String translateToTalhamr(String[] wordArray, String[] wordBank) {
for (int i=0; i < wordArray.length; i++) {
System.out.print("'");
for (int j = 0; j < wordBank.length; j++) {
System.out.print(".");
if (wordBank[j] == wordArray[i]) {
System.out.print(",");
wordArray[i] = wordBank[j + 1];
} else {
wordArray[i] = wordArray[i] + "!";
}
}//end of inner word bank
}//end of outer for loop
System.out.println("\n" + wordArray.toString());
return wordArray.toString();
}//end of translateToTalhamr
public static void main(String[] args) {
}//end of main method
}//end of EtoT发布于 2016-02-20 20:43:23
您的问题是使用==比较字符串。因为String是对象,所以检查它们是否是同一个对象,而不是检查它们是否有相同的文本。要正确测试这一点,可以使用string1.equals(string2)
对于您来说,在translateToTalhamr函数中,您应该使用wordBank[j].equals(wordArray[i])而不是wordBank[j] == wordArray[i]。
https://stackoverflow.com/questions/35528428
复制相似问题