本文实例为大家分享了Java实现中英文词典功能的具体代码,供大家参考,具体内容如下
功能如下:
1、可以向词典中增加中英文单词,并提供修改和删除功能
2、完成输入中文查询英文的功能,如果没有提示没有查到
3、完成输入英文查询中文的功能,如果没有提示没有查到
4、功能的选择使用文字菜单完成,每个操作要求有明确的操作提示和结果演示,以及结果演示的暂停。
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
import java.util.Objects; import java.util.HashMap; import java.util.Scanner; //单词模型类 class Word { private String name; private String meaning; public Word() { } public Word(String name, String meaning) { this .name = name; this .meaning = meaning; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getMeaning() { return meaning; } public void setMeaning(String meaning) { this .meaning = meaning; } @Override public boolean equals(Object o) { if ( this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Word word = (Word) o; return Objects.equals(name, word.name) && Objects.equals(meaning, word.meaning); } @Override public int hashCode() { return Objects.hash(name, meaning); } } //CURD class Dictionary { private HashMap<String, Word> ch = null ; private HashMap<String, Word> en = null ; //初始化 public Dictionary() { this .ch = new HashMap<String, Word>(); this .en = new HashMap<String, Word>(); } //添加中文单词 public void addChWord() { System.out.println( "请输入单词名称" ); Scanner sc = new Scanner(System.in); String name = sc.next(); if (ch.containsKey(name)) { System.out.println( "这个单词已存在,请重试" ); } else { System.out.println( "请输入这个单词的英文翻译" ); String meaning = sc.next(); ch.put(name, new Word(name, meaning)); System.out.println( "添加成功" ); } backMenu(); } //添加英文单词 public void addEnWord() { System.out.println( "请输入单词名称" ); Scanner sc = new Scanner(System.in); String name = sc.next(); if (en.containsKey(name)) { System.out.println( "这个单词已存在,请重试" ); } else { System.out.println( "请输入这个单词的中文翻译" ); String meaning = sc.next(); en.put(name, new Word(name, meaning)); System.out.println( "添加成功" ); } backMenu(); } //改变中文单词 public void changeCh() { System.out.println( "请输入您要修改的中文单词" ); Scanner sc = new Scanner(System.in); String name = sc.next(); boolean flag = ch.containsKey(name); if (!flag) { System.out.println( "您要修改的中文单词不存在,请输入任意字母返回" ); sc.next(); } else { System.out.println( "请输入修改后的内容" ); String meaning = sc.next(); ch.put(name, new Word(name, meaning)); System.out.println( "修改成功" ); } backMenu(); } //改变英文单词 public void changeEn() { System.out.println( "请输入您要修改的英文单词" ); Scanner sc = new Scanner(System.in); String name = sc.next(); boolean flag = en.containsKey(name); if (!flag) { System.out.println( "您要修改的英文单词不存在,请输入任意字母返回" ); sc.next(); } else { System.out.println( "请输入修改后的内容" ); String meaning = sc.next(); en.put(name, new Word(name, meaning)); System.out.println( "修改成功" ); } backMenu(); } //删除中文单词 public void deleteCh() { System.out.println( "请输入您要删除的中文单词" ); Scanner sc = new Scanner(System.in); String name = sc.next(); if (ch.remove(name) != null ) System.out.println( "删除成功" ); else System.out.println( "您要删除的单词不存在,请重试" ); backMenu(); } //删除英文单词 public void deleteEn() { System.out.println( "请输入您要删除的英文单词" ); Scanner sc = new Scanner(System.in); String name = sc.next(); if (en.remove(name) != null ) System.out.println( "删除成功" ); else System.out.println( "您要删除的单词不存在,请重试" ); backMenu(); } //查询中文单词的翻译 public void seachEn() { System.out.println( "请输入您要查询的中文单词" ); Scanner sc = new Scanner(System.in); String name = sc.next(); boolean flag = ch.containsKey(name); if (!flag) { System.out.println( "您要查询的中文单词不存在,请重试" ); } else { Word word = ch.get(name); System.out.println(name + "的英文翻译是" + word.getMeaning()); } backMenu(); } //查询英文单词的中文翻译 public void seachCh() { System.out.println( "请输入您要查询的英文单词" ); Scanner sc = new Scanner(System.in); String name = sc.next(); boolean flag = en.containsKey(name); if (!flag) { System.out.println( "您要查询的英文单词不存在,请重试" ); } else { Word word = en.get(name); System.out.println(name + "的中文翻译是" + word.getMeaning()); } backMenu(); } public void backMenu() { System.out.println( "请输入9返回菜单" ); Scanner sc = new Scanner(System.in); sc.next(); System.out.println(); } } //测试类 public class MainClass { public static void main(String[] args) { Dictionary myDictionary = new Dictionary(); while ( true ) { System.out.println( "————————欢迎使用中英文词典系统————————" ); System.out.println( "1.添加中文单词" ); System.out.println( "2.添加英文单词" ); System.out.println( "3.修改中文单词" ); System.out.println( "4.修改英文单词" ); System.out.println( "5.删除中文单词" ); System.out.println( "6.删除英文单词" ); System.out.println( "7.查询中文单词翻译" ); System.out.println( "8.查询英文单词翻译" ); System.out.println( "9.退出系统" ); System.out.println( "请输入你的选择:" ); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); switch (i) { case 1 : myDictionary.addChWord(); break ; case 2 : myDictionary.addEnWord(); break ; case 3 : myDictionary.changeCh(); break ; case 4 : myDictionary.changeEn(); break ; case 5 : myDictionary.deleteCh(); break ; case 6 : myDictionary.deleteEn(); break ; case 7 : myDictionary.seachEn(); break ; case 8 : myDictionary.seachCh(); break ; case 9 : System.out.println( "谢谢使用" ); System.exit( 0 ); } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_51119270/article/details/120317366