java英语词典管理程序
时间: 2023-12-18 18:29:43 浏览: 95
java电子词典程序
4星 · 用户满意度95%
以下是一个简单的Java英语词典管理程序的示例:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class DictionaryManager {
private Map<String, String> dictionary;
public DictionaryManager() {
dictionary = new HashMap<>();
}
public void addWord(String word, String definition) {
dictionary.put(word, definition);
}
public String getDefinition(String word) {
return dictionary.get(word);
}
public void removeWord(String word) {
dictionary.remove(word);
}
public void printAllWords() {
for (String word : dictionary.keySet()) {
System.out.println(word);
}
}
public static void main(String[] args) {
DictionaryManager manager = new DictionaryManager();
Scanner scanner = new Scanner(System.in);
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. 退出");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入单词:");
String word = scanner.nextLine();
System.out.println("请输入定义:");
String definition = scanner.nextLine();
manager.addWord(word, definition);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入单词:");
word = scanner.nextLine();
definition = manager.getDefinition(word);
if (definition != null) {
System.out.println(definition);
} else {
System.out.println("未找到该单词!");
}
break;
case 3:
System.out.println("请输入单词:");
word = scanner.nextLine();
manager.removeWord(word);
System.out.println("删除成功!");
break;
case 4:
manager.printAllWords();
break;
case 5:
System.exit(0);
default:
System.out.println("无效的选择!");
}
}
}
}
```
该程序使用了HashMap来存储单词和定义的对应关系,并提供了添加、查找、删除和列出所有单词的功能。用户可以通过控制台输入选择来执行相应的操作。
阅读全文