猿问

树集未添加所有对象

我正在为越南语写字典,但我的 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;

}


@Override

public 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 nha


2名十


慕工程0101907
浏览 100回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答