我正在为越南语写字典,但我的 Treeset 只添加了 1 个对象。我一直在寻找 2 天,但我无法弄清楚如何。希望你们帮助我。public class Word implements Comparable<Word> {private static String word_target, word_explain;public static void setWord_target(String word_target) { Word.word_target = word_target;}public static void setWord_explain(String word_explain) { Word.word_explain = word_explain;}public String getWord_explain() { return word_explain;}public String getWord_target() { return word_target;}@Overridepublic int compareTo(Word word) { return this.getWord_target().compareTo(word.getWord_target());}}public class Dictionary {private TreeSet<Word> words = new TreeSet<Word>();public TreeSet<Word> getWords() { return words;}}public class DictionaryManagement {static Scanner reader = new Scanner(System.in);public static int numbers;public static void insertFromCommandline(Dictionary dic) { numbers = reader.nextInt(); reader.nextLine(); for (int i = 0; i < numbers; i++) { Word putInWord = new Word(); String en_word, vn_word; System.out.print("English Word: "); en_word = reader.nextLine(); putInWord.setWord_target(en_word); System.out.print("VietNameses Word: "); vn_word = reader.nextLine(); putInWord.setWord_explain(vn_word); dic.getWords().add(putInWord); }}}public class DictionaryCommandline {private static int num = 1;public static Dictionary showWord = new Dictionary();public static void showAllWords() { System.out.println("No |English |Vietnamese"); for (Word wr : showWord.getWords()) { System.out.println( num++ + " |" + wr.getWord_target() + " |" + wr.getWord_explain()); }}例子:输入:2英文单词:房子越南语词:义芽英文单词:姓名越南语词:十- 实际输出:没有英语越南1名十- 预期输出:没有英语越南1 房子 ngoi nha2名十
添加回答
举报
0/150
提交
取消