感兴趣的小伙伴可以先看看我的这篇文章哦,打开看看,说不定能帮到你一些~~
要求如下:
- 可以向词典中增加中英文单词,并提供修改和删除功能
- 完成输入中文查询英文的功能,如果没有提示没有查到
- 完成输入英文查询中文的功能,如果没有提示没有查到
- 功能的选择使用文字菜单完成,每个操作要求有明确的操作提示和结果演示,以及结果演示的暂停。
package com.zhangyufan.test;
import java.util.HashMap;
import java.util.Objects;
import java.util.Scanner;
public class TestDictionary {
public static void main(String[] args) {
Dictionary dictionary = 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:
dictionary.addChWord();
break;
case 2:
dictionary.addEnWord();
break;
case 3:
dictionary.modifyCh();
break;
case 4:
dictionary.modifyEn();
break;
case 5:
dictionary.deleteCh();
break;
case 6:
dictionary.deleteEn();
break;
case 7:
dictionary.ceTranslation();
break;
case 8:
dictionary.ecTranslation();
break;
case 9:
System.out.println("谢谢使用本系统,欢迎下次使用!");
System.exit(0);
default:
break;
}
}
}
}
class Word {
private String name1;
private String name2;
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public Word(String name1, String name2) {
super();
this.name1 = name1;
this.name2 = name2;
}
public Word() {
super();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || this.getClass() != o.getClass())
return false;
Word word = (Word) o;
return Objects.equals(this.name1, word.name1) && Objects.equals(this.name2, word.name2);
}
@Override
public int hashCode() {
return Objects.hash(name1, name2);
}
}
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 static void clear() {
for (int i = 0; i < 30; i++) {
System.out.println();
}
}
public void addChWord() {
System.out.println("请输入您想要添加的中文单词:");
Scanner sc = new Scanner(System.in);
String name1 = sc.next();
if (ch.containsKey(name1)) {
System.out.println("该中文单词已存在,添加失败!");
} else {
System.out.println("请输入该中文单词的英文翻译:");
String name2 = sc.next();
ch.put(name1, new Word(name1, name2));
System.out.println("中文单词“" + name1 + "”添加成功!");
}
backMenu();
}
public void addEnWord() {
System.out.println("请输入您想要添加的英文单词:");
Scanner sc = new Scanner(System.in);
String name1 = sc.next();
if (en.containsKey(name1)) {
System.out.println("该英文单词已存在,添加失败!");
} else {
System.out.println("请输入该英文单词的中文翻译:");
String name2 = sc.next();
en.put(name1, new Word(name1, name2));
System.out.println("英文单词“" + name1 + "”添加成功!");
}
backMenu();
}
public void modifyCh() {
System.out.println("请输入您想要修改的中文单词");
Scanner sc = new Scanner(System.in);
String name1 = sc.next();
boolean flag = ch.containsKey(name1);
if (!flag) {
System.out.println("您想要修改的中文单词不存在!");
} else {
System.out.println("确认修改? 请输入y(代表Yes)或n(代表No)");
String yesNo = sc.next().toLowerCase();
if ("y".equals(yesNo)) {
System.out.println("请输入该中文单词修改后的英文翻译");
String name2 = sc.next();
ch.put(name1, new Word(name1, name2));
System.out.println("中文单词“" + name1 + "”修改成功!");
} else {
System.out.println("取消修改!");
}
}
backMenu();
}
public void modifyEn() {
System.out.println("请输入您想要修改的英文单词");
Scanner sc = new Scanner(System.in);
String name1 = sc.next();
boolean flag = en.containsKey(name1);
if (!flag) {
System.out.println("您想要修改的英文单词不存在!");
} else {
System.out.println("确认修改? 请输入y(代表Yes)或n(代表No)");
String yesNo = sc.next().toLowerCase();
if ("y".equals(yesNo)) {
System.out.println("请输入该英文单词修改后的中文翻译");
String name2 = sc.next();
en.put(name1, new Word(name1, name2));
System.out.println("英文单词“" + name1 + "”修改成功!");
} else {
System.out.println("取消修改!");
}
}
backMenu();
}
public void deleteCh() {
System.out.println("请输入您想要删除的中文单词");
Scanner sc = new Scanner(System.in);
String name = sc.next();
boolean flag = ch.containsKey(name);
if (!flag)
System.out.println("您想要删除的中文单词不存在!");
else {
System.out.println("确认删除? 请输入y(代表Yes)或n(代表No)");
String yesNo = sc.next().toLowerCase();
if ("y".equals(yesNo)) {
ch.remove(name);
System.out.println("中文单词“" + name + "”删除成功!");
} else {
System.out.println("取消删除!");
}
}
backMenu();
}
public void deleteEn() {
System.out.println("请输入您想要删除的英文单词");
Scanner sc = new Scanner(System.in);
String name = sc.next();
boolean flag = en.containsKey(name);
if (!flag)
System.out.println("您想要删除的英文单词不存在!");
else {
System.out.println("确认删除? 请输入y(代表Yes)或n(代表No)");
String yesNo = sc.next().toLowerCase();
if ("y".equals(yesNo)) {
en.remove(name);
System.out.println("英文单词“" + name + "”删除成功!");
} else {
System.out.println("取消删除!");
}
}
backMenu();
}
public void ceTranslation() {
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.getName2());
}
backMenu();
}
public void ecTranslation() {
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.getName2());
}
backMenu();
}
public void backMenu() {
System.out.println("请输入任意字符返回主菜单!");
Scanner sc = new Scanner(System.in);
sc.next();
clear();
}
}