练习题.实现简单的电子词典功能

这是一篇由Java初学者编写的博客,旨在实现简单的电子词典功能。文章中展示了作者的初始尝试及后续的改进,包括错误检查和答案展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java初学者所写,有待改进

【题目】.实现简单的电子词典功能。

a.能记录新的单词和解释。
b.能查询单词的解释。
c.能从单词库中随机抽取10个单词,进行测试。(可以是显示单词,要求输入解释,也可反过来)
  最后给出对错的个数和正确答案。

d.所有操作在控制台,用命令完成。
提示:使用Properties,并把添加的单词永久保持到文件中。

举例:
c:\>java MyWord
c:\>请选择功能:1-添加单词,2-查询单词,3-单词测试,0-退出
c:\>1
c:\>请输入单词:
c:\>cat
c:\>请输入解释:
c:\>猫
c:\>成功添加单词。是否继续?Y/N
如果 c:\>Y -> 13行
如果 c:\>N -> 11行

c:\>2
c:\>请输入单词:
c:\>cat
c:\>cat的解释为:猫

c:\>3
c:\>cat的解释:
c:\>猫
c:\>狗的单词是:
c:\>dog

...最后给出对了多少个,并把错误的答案显示。

第二份代码为修改后的。

代码如下:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.zip.Inflater;

public class DictionaryTest {

	public static void main(String[] args) throws IOException {
		
		WordManage wm = new WordManage();
		Scanner sc = new Scanner(System.in);
		while(true){
			System.out.println("\tjava MyWord");
			System.out.println("1.增添单词");
			System.out.println("2.查询单词");
			System.out.println("3.单词测试");
			System.out.println("0.安全退出");
			System.out.println("请输入相应的数字进行对应的操作:");
			int num = sc.nextInt();
			if(num == 1){
				wm.addWord();
			}else if(num == 2){
				wm.query();
			}else if(num == 3){
				wm.test();
			
			}else if(num ==0){
				System.out.println("已正常退出!");
				System.exit(0);
			}else{
				System.out.println("输入错误,请重新输入!");
			}
		}
	}
}

class WordManage{
	
	private Properties prop ;
	//创建构造函数,初始化成员变量
	public WordManage() throws IOException{
		prop = new Properties();
	}
	
	//加入单词
	public void addWord() throws IOException{
		InputStream is = new FileInputStream("D:/test/word.txt"); 
		prop.load(is);
		OutputStream os = new FileOutputStream("D:/test/word.txt"); 
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入单词:");
		String str = sc.next();
		System.out.print("请输入解释:");
		String explain = sc.next();
		prop.put(str,explain);
		prop.store(os, "My save");
		os.close(); 
		is.close();
		System.out.println("成功加入单词,是否继续?Y/N");
		String yn = sc.next();
		if(yn.equals("Y")){
			//加单词
			addWord();
		}
	}
	
	//查询单词
	public void query() throws IOException{
		System.out.println("请输入单词:");
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		InputStream is = new FileInputStream("D:/test/word.txt");
		prop.load(is);
		int flag = 0;
		for (Object key : prop.keySet()) { 
			if(str.equals(key)){
				System.out.println(key + "的解释为:" + prop.get(key)); 
				flag = 1;
				break;
			}
		}
		if(flag == 0){
			System.out.println("词库没有此单词!");
		}
	}
	
	//单词测试
	public void test() throws IOException{
		InputStream is = new FileInputStream("D:/test/word.txt");
		prop.load(is);
		 Set<Object> key = prop.keySet();
		 int size = key.size();
		 ArrayList al= new ArrayList();
		 ArrayList<String> wroAnswer =new ArrayList<String>(); 
		 Random rd = new Random();
		 for(int i=0; i<5; i++){
			 int data = rd.nextInt(size);
			 while(al.contains(data)){
				 data = rd.nextInt(size);
			 }
			 al.add(data);
		 }
		 int rightTotal = 0;
		 int t = rd.nextInt(size);
		for(int i = 0; i<5; i++){
			Random r = new Random();
			int temp = r.nextInt(2);
			int bk = 0;
			int j = (Integer) al.get(i);
			if(temp == 0){
				//cat的解释:
				for (Object k : prop.keySet()) { 
					if(j == bk){
						System.out.println(k + "的解释:");
						Scanner sc = new Scanner(System.in);
						String explain = sc.next();
						if(explain.equals(prop.get(k))){
							rightTotal++;
						}else{
							wroAnswer.add(k+"的正确解释为:"+prop.get(k));
						}
						break;
					}
					bk++;
				}
			}else{
				//狗的单词是:
				for (Object k : prop.keySet()) { 
					if(j == bk){
						System.out.println(prop.get(k)+ "的单词是:");
						Scanner sc = new Scanner(System.in);
						String str = sc.next();
						if(str.equals(k)){
							rightTotal++;
						}else{
							wroAnswer.add(prop.get(k)+ "的正确单词是:"+k);
						}
						break;
					}
					bk++;
				}
			}
		}
		System.out.println("您答对的个数为:"+rightTotal+"个");
		if(rightTotal!=5){
			System.out.println("纠错如下:");
			/*for(String k: wroAnswer){
				System.out.println(k);
			}*/
			System.out.println(wroAnswer);
		}
	}
		
}



修改后代码r如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Dictionary {

	private static Properties pps = new Properties();
	private File file;
	
	public Dictionary(){
		file = new File("src/res/words.properties");
		loadProperties();
	}

	// 1.实现通过控制台添加新的单词
	public void addWord(Scanner sc) {
		boolean b = true;
		while (b) {
			System.out.println("请输入单词:");
			String key = sc.nextLine();
			System.out.println("请输入解释:");
			String value = sc.nextLine();
			if (pps.containsKey(key)) {
				System.out.println("单词已经存在!请从新输入。");
			} else {
				pps.setProperty(key, value);
				System.out.print("成功添加单词。");
				while (true) {
					System.out.println("是否继续?Y/N");
					String yn = sc.nextLine();
					if ("N".equals(yn)) {
						b = false;
						break;
					} else if ("Y".equals(yn)) {
						break;
					} else {
						System.out.println("输入有误!");
					}
				}
			}
		}

	}
	
	//2.查询单词
	public void findWord(Scanner sc){
		boolean b = true;
		while(b){
			System.out.println("请输入单词:");
			String key = sc.nextLine();
			String value = pps.getProperty(key);
			if(value != null){
				System.out.println(key + "单词的解释为:" + value);
			}else{
				System.out.println("没有找到此单词!");
			}
			
			while (true) {
				System.out.println("是否继续?Y/N");
				String yn = sc.nextLine();
				if ("N".equals(yn)) {
					b = false;
					break;
				} else if ("Y".equals(yn)) {
					break;
				} else {
					System.out.println("输入有误!");
				}
			}
		}
	}
	
	//3.单词测试
	public void testWords(Scanner sc){
		Set<String> keys = pps.stringPropertyNames();
		ArrayList<String> list = new ArrayList<String>(keys);
		Collections.shuffle(list);
		
		//保存正确的个数
		int count = 0;
		//保持错误的键值字符串
		StringBuilder sbd = new StringBuilder();
		
		//
		Random rn = new Random();
		for(int i=0; i<10 && i<list.size(); i++){
			String key = list.get(i);
			String value = pps.getProperty(key);
			int num = rn.nextInt(10);
			if( (num % 2) == 0){
				System.out.println(key + "的对应解释是:");
				String s = sc.nextLine();
				if(value.equals(s)){
					count ++;
				}else{
					sbd.append(key);
					sbd.append("=");
					sbd.append(value);
					sbd.append(", ");
				}
			}else{
				System.out.print(value + "的对应单词是:" );
				String s = sc.nextLine();
				if(key.equals(s)){
					count ++;
				}else{
					sbd.append(key);
					sbd.append("=");
					sbd.append(value);
					sbd.append(", ");
				}
			}
		}
		System.out.println("正确的个数为:" + count);
		System.out.println("错误的单词解释为:");
		System.out.println(sbd);

	}
	
	

	// 保持属性列表中的内容到文件中
	public void saveProperties() {

		FileOutputStream fos = null;
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			fos = new FileOutputStream(file);
			pps.store(fos, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	// 从文件中加载属性列表
	public void loadProperties() {
		try {
			if (file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				pps.load(fis);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		Dictionary hw = new Dictionary();
		boolean b = true;
		Scanner sc = new Scanner(System.in);
		while (b) {
			System.out.println("请选择功能:1-添加单词,2-查询单词,3-单词测试,0-退出");
			int s1 = sc.nextInt();
			switch (s1) {
			case 1:
				hw.addWord(sc);
				break;
			case 2:
				hw.findWord(sc);
				break;
			case 3:
				hw.testWords(sc);
				break;
			case 0:
				b = false;
				hw.saveProperties();
				System.out.println("成功退出程序!");
				break;
			default:
				System.out.println("输入有误!请重新选择。");
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值