2 回答

TA贡献1780条经验 获得超4个赞
我如何随机访问我的字符串数组答案列表?(每次都像不同的(顺序)问题)。
由于您想更改测验/随机等问题,建议您将问题和答案映射在一起。洗牌问题列表和答案列表不是首选,也没有解决方案。
我以为我看到您可以使用(数组)列表代替我现在使用的字符串数组。如果这是真的,请您解释一下如何做到这一点。以及如何随机访问。
以及如何将随机访问的答案与为用户显示的问题相匹配?我读了一些关于为此使用类的信息?
Hashmap在 Java 中使用以下示例。
public class QuestionAnswers {
public static void main(String[] args) {
HashMap<String, String> questionSets = new HashMap<String, String>();
questionSets.put("Question1", "Answer1");
questionSets.put("Question2", "Answer2");
questionSets.put("Question3", "Answer3");
questionSets.put("Question4", "Answer4");
questionSets.put("Question5", "Answer5");
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(questionSets.entrySet());
System.out.println("************ Questions ************");
Collections.shuffle(list);
for (Map.Entry<String, String> entry : list) {
System.out.println(entry.getKey());
// Here entry.getKey() is Question and if user enters the correct answer
// match that answer with like -> "user_answer".equals(entry.getValue());
}
}
}
添加回答
举报