package englishtest;
import java.util.*;
public class ruko {
private static String[] keys;
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("I","我");
map.put("am","是(第一人称)");
map.put("aah","啊");
map.put("hello","你好");
map.put("hi","嗨");
map.put("ooh","嗬");
map.put("goodbye","再见");
map.put("bye-bye","拜拜");
map.put("are","是(第二人称)");
map.put("good","好的");
Scanner scan = new Scanner(System.in);
Random rand = new Random();
System.out.println("欢迎使用外研版英语单词记忆测评系统V1.0.0");
System.out.println("‘1’为三年级");
System.out.println("‘2’为四年级");
System.out.println("‘3’为五年级");
System.out.println("‘4’为六年级");
System.out.print("请输入:");
String a = scan.next();
int fan = 100;
int go = 1;
for (;go <= 100;go += 1) {
if (a.equals(1)) {
//23333
}
}
if (!a.equals("1") && !a.equals("2") && !a.equals("3") && !a.equals("4")) {
if (!a.equals("5") && !a.equals("6") && !a.equals("7") && !a.equals("8")) {
System.out.println("错误!");
}
}
System.out.println("得分;" + fan);
}
}
我想在//23333的地方随机选一个map.put中的数据,输出“(后面的中文)的英文单词是:”,然后让用户输入对应的英文(即前面的英文),然后比较用户的输入和数据,判断是否一样。
但是随机的部分怎么也做不好,有没有人教教我啊?
附:参考资料(网上找的,数据类型有问题):
package pre;
import java.util.*;
public class pre {
public static void main(String[] args) {
Map map = new HashMap();
map.put(33, 333);
map.put(123, 1234);
map.put(321, 4321);
map.put(555,5555);
Integer[] keys = map.keySet().toArray(new Integer[0]);
Random random = new Random();
Integer randomKey = keys[random.nextInt(keys.length)];
System.out.println(randomKey);
}
}
把数据类型改成String[]就行了,因为你的map的key是String类型
原有代码添加获取随机key的方法:
// 将map中存放的key组装成字符串数组。下面数组中自行补充剩余map中的key
private static String[] keyArray={"I","amm"};
// 获取随机的map中的key
public String getrandomKey(){
Random random = new Random();
// 获取数组长度随机数,取值范围是0-数组长度(不包含数组长度值)
int keyArrayIndex = random.nextInt(keyArray.length);
// 根据获取的随机数作为数组的索引,获取数组中对应的key值
String randomKey=keyArray[keyArrayIndex];
return randomKey;
}
如有帮助,欢迎采纳!