编写一个记单词 和默写单词两个功能的小程序
java
package com.lu.word;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class A {
private String k;
private String v;
}
java
package com.lu.word;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class English {
//单词计分
private static int scoring = 0;
public static void main(String[] args) {
while (true) {
System.out.println("1.开始背单词 2.默写单词中文意思 3.退出");
Scanner scanner = new Scanner(System.in);
int num = 0;
while (true) {
try {
num = scanner.nextInt();
} catch (Exception e) {
System.out.println("输入错误,请重新输入");
scanner.nextLine(); // 清除错误的输入
continue;
}
break;
}
switch (num) {
case 1:
System.out.println("开始背单词");
Properties properties = new Properties();
try(FileReader fileReader = new FileReader("resource/work.properties");){
properties.load(fileReader);
properties.forEach((k,v)->{
try {
Thread.sleep(2000);
System.out.println(k+" : "+v);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case 2:
Properties properties1 = new Properties();
try (FileReader fileReader = new FileReader("resource/work.properties");) {
properties1.load(fileReader);
System.out.println("开始默写单词中文意思");
HashMap<Integer, A> map = new HashMap<>();
final int[] a = {1};
properties1.forEach((k, v) -> map.put(a[0]++, new A((String) k, (String) v)));
//判断生成的随机数是否在这个数组中
int[] arr =new int[10];
//随机数对象
Random random = new Random();
//生成一个1到map的长度之间的随机数
for (int j = 0; j <10 && j< map.size() ; j++) {
int i;
//判断生成的随机数是否在这个数组中
while (true){
i = random.nextInt(1,map.size()+1);
//判断生成的随机数是否在这个数组中
if (!WordService.contains(arr, i)) {
break;
}
}
for (int k = 0; k < 3; k++) {
System.out.println("请输入单词翻译");
System.out.println(map.get(i).getK());
String next = scanner.next();
if (next.equals(map.get(i).getV())) {
scoring += 10;
System.out.println("正确");
break;
} else {
System.out.println("错误");
}
}
arr[j] = i;
}
System.out.println("你的得分是:"+scoring);
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case 3:
System.out.println("退出");
System.exit(0);
break;
default:
System.out.println("输入错误,请重新输入");
break;
}
}
}
}
javascript
package com.lu.word;
public class WordService {
/**
* 判断数组中是否包含某个数字
* @param arr 数组
*/
public static boolean contains(int[] arr, int num) {
for (int j : arr) {
if (j == num) {
return true;
}
}
return false;
}
}