[20-05-02][Self-test 31]Java Dictionary

Posted mirai3usi9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[20-05-02][Self-test 31]Java Dictionary相关的知识,希望对你有一定的参考价值。

 1 package test_7_1;
 2 
 3 public class DicData {
 4 
 5     /**
 6      * 设计一个词典类Dic,每个单词包括英文单词及对应的中文含义,
 7      * 并有一个英汉翻译成员函数,通过查词典的方式将一段英语翻译成对应的汉语。
 8      */
 9     
10     public String chinese;
11     public String english;
12     
13     public String getChinese() {
14         
15         return chinese;
16     }
17     
18     public String getEnglish() {
19         
20         return english;
21     }
22     
23     public void setChinese(String chinese) {
24         
25         this.chinese = chinese;
26     }
27     
28     public void setEnglish(String english) {
29         
30         this.english = english;
31     }
32 }

 

 1 package test_7_1;
 2 
 3 public class Dic {
 4 
 5     DicData[] dic = new DicData[10];
 6     
 7     public static int index = 0;
 8     
 9     public void add(String chinese, String english) {
10         
11         DicData word = new DicData();
12         
13         word.setChinese(chinese);
14         word.setEnglish(english);
15 
16         dic[index] = word;
17         index++;
18         
19     }
20     
21     public void transfer(String word) {
22         
23         for (int i = 0; i < index; i++) {
24             if (word.equals((dic[i].getChinese()))) {
25                 System.out.println(word + " : " + dic[i].getEnglish());
26                 return;
27             } else if (word.equals((dic[i].getEnglish()))) {
28                 System.out.println(word + " : " + dic[i].getChinese());
29                 return;
30             } 
31         }
32         
33         System.out.println("查无此词");
34     }
35 
36 }

 

 1 package test_7_1;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6 
 7         Dic dic = new Dic();
 8         
 9         dic.add("小丑", "joker");
10         dic.add("骷髅", "skull");
11         dic.add("豹", "panther");
12         dic.add("狐狸", "fox");
13         dic.add("女王", "queen");
14         
15         dic.transfer("joker");
16         dic.transfer("骷髅");
17         dic.transfer("violet");
18         dic.transfer("导航");
19     }
20 
21 }

 

结果如下:

joker : 小丑
骷髅 : skull
查无此词
查无此词

以上是关于[20-05-02][Self-test 31]Java Dictionary的主要内容,如果未能解决你的问题,请参考以下文章

[20-05-02][Self-test 32]Java GuessNum

[20-05-02][Self-test 35]Java OverRide

[20-04-26][Self-test 6]Java CharType

[20-05-09][Self-test 40]Java BankSystem 1

[20-05-09][Self-test 40]Java BankSystem 1

[20-06-04][Self-test 46]Java Linker Manage