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());
}
}
}
添加回答
舉報