[英]How to most efficiently choose 3 random questions and sort them in a random order?
我正在為一個學校項目工作,並被要求創建一個“商店”來出售愚蠢的物品。 用戶將適當數量的項目添加到他/她的購物車后,他們選擇結帳選項。 之后,根據《星球大戰》知識向用戶提示瑣事問題。
為了獲得班上的積分,我需要創建一個文件,其中存儲了幾個問題和答案,然后檢查它們是否正確。
我目前有2個文件,一個trivaQ.txt和一個triviaA.txt。 他們都持有“ Question1”-“ Question10”和“ Answer1”-“ Answer10”。 Question1的答案為Answer1,Question2的答案為Answer2,依此類推。
我編寫了一個可以訪問文件的簡單ReadFile類,並且嘗試構建一種方法,該方法將選擇正確的答案(與問題索引相同),然后選擇其他2個隨機答案。
這是當前代碼:
public int askA(){
int cAnswer = Question;
int answer1= (int)(Math.random() *10);
int answer2= (int)(Math.random() *10);
while(answer1 == cAnswer || answer1 == answer2){
answer1 = (int)(Math.random() *10);
}
while(answer2 == cAnswer || answer2 == answer1){
answer2 = (int)(Math.random() *10);
}
int x = random.nextInt(3)+1;
int y = random.nextInt(3)+1;
int z = random.nextInt(3)+1;
while( x == y || x == z){
x = random.nextInt(3)+1;
}
while( y == x || y == z){
y = random.nextInt(3)+1;
}
while( z == x || z == y){
z = random.nextInt(3)+1;
}
if(x > y && x > z){
//x is first
if(y > z){
//y is second
//z is third
System.out.println("[1.] " + itemData.get(cAnswer));
System.out.println("[2.] " + itemData.get(answer1));
System.out.println("[3.] " + itemData.get(answer2));
System.out.println("Answer is 1");
}else{
//z is second
//y is third
System.out.println("[1.] " + itemData.get(cAnswer));
System.out.println("[2.] " + itemData.get(answer2));
System.out.println("[3.] " + itemData.get(answer1));
System.out.println("Answer is 1");
}
}else if(y > x && y > z){
//y is first
if(x > z){
//x is second
//z is third
System.out.println("[1.] " + itemData.get(answer1));
System.out.println("[2.] " + itemData.get(cAnswer));
System.out.println("[3.] " + itemData.get(answer2));
System.out.println("Answer is 2");
}else{
//z is second
//x is third
System.out.println("[1.] " + itemData.get(answer1));
System.out.println("[2.] " + itemData.get(answer2));
System.out.println("[3.] " + itemData.get(cAnswer));
System.out.println("Answer is 2");
}
}else if(z > y && z > x){
//z is first
if(y > x){
//y is second
//x is third
System.out.println("[1.] " + itemData.get(answer2));
System.out.println("[2.] " + itemData.get(answer1));
System.out.println("[3.] " + itemData.get(cAnswer));
System.out.println("Answer is 3");
}else{
//x is second
//y is third
System.out.println("[1.] " + itemData.get(answer2));
System.out.println("[2.] " + itemData.get(cAnswer));
System.out.println("[3.] " + itemData.get(answer1));
System.out.println("Answer is 3");
}
}
System.out.println("X is - " + x);
System.out.println("CorrectAnswer --- " + cAnswer);
return x;
}
該方法目前無法正常運行。 它選擇一個問題,然后選擇正確的答案和2個隨機答案。 它沒有按照正確的順序對它們進行排序。
我檢查返回的值x是否等於用戶輸入,雖然它可能工作6/10次,但失敗4/10次。
有沒有更有效的方法可以做到這一點? 我的代碼出了什么問題?
簡單如:
java.util.Collections.shuffle(itemData);
result = FluentIterable.from(itemData).limit(3).toList(); //(using Google Guava)
result = itemData.stream().limit(3).collect(Collectors.toList());//(using java 8):
調用隨機播放后,您也可以從
itemData.get(0)
itemData.get(1)
itemData.get(2)
對於我來說,無需使用Google番石榴等就能使其正常工作的最簡單方法是創建另一個整數reAnswer。
int reAnswer = 0;
每次之后:
//z is second
//y is third
System.out.println("[1.] " + itemData.get(cAnswer));
System.out.println("[2.] " + itemData.get(answer2));
System.out.println("[3.] " + itemData.get(answer1));
System.out.println("Answer is 1");
我會補充:
reAnswer = 1;
或與正確答案相對應的任何答案。 然后,我從方法中返回值reAnswer並在主文件中進行了檢查。 這是一個簡單的解決方法。 謝謝大家的幫助,直到現在我還沒有考慮過這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.