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("输入有误!请重新选择。");
}
}
}
}