2016-03-07 70 views
0

我想創建一個簡單的問卷調查程序 - 將會有五個問題和四個答案。每次運行時,問題和答案的順序都是隨機的。簡單的問卷調查程序

例:

第一次運行:

  1. 什麼是果紅色? a。蘋果b.Pineapple c。橙色d。梨

  2. 什麼形狀有三面? a。廣場b。圈子 c。三角形d。八角

第二次運行:

  1. 什麼形狀有三面? a。八角形b。廣場 c。圈子d。三角形
  2. 什麼是水果紅色? a。橙色b。蘋果 c。梨d。菠蘿

- 編輯 - 這是我根據提出的建議守則草案:

public static void main(String[] args) { 
    Scanner input = new Scanner (System.in); 
    Random rand = new Random(); 

    ArrayList<ArrayList<String>> arrList = new ArrayList<ArrayList<String>>(); 
    ArrayList<String> question = new ArrayList<String>(); 
    ArrayList<String> choiceShuffle = new ArrayList<String>(); 
    ArrayList<String> choice1 = new ArrayList<String>(); 
    ArrayList<String> choice2 = new ArrayList<String>(); 
    ArrayList<String> choice3 = new ArrayList<String>(); 

    int numQuestion = 3; 
    int randomNum = rand.nextInt(numQuestion); 
    int score = 0; 
    String questionShuffle; 
    questionShuffle = question.get(randomNum); 
    choiceShuffle = arrList.get(randomNum); 

    question.add("What is after a?"); 
    question.add("What is after b?"); 
    question.add("What is after c?"); 

    choice1.add("a"); choice1.add("b"); choice1.add("c"); 
    choice2.add("d"); choice2.add("c"); choice2.add("e"); 
    choice3.add("d"); choice3.add("a"); choice3.add("b"); 

    arrList.add(choice1); 
    arrList.add(choice2); 
    arrList.add(choice3); 

    for (int x = 1; x <= question.size(); x++){ 
     System.out.print(question.get(randomNum)); 
     Collections.shuffle(choiceShuffle); 
     System.out.println(choiceShuffle); 
    } 
} 

}

我要去哪裏錯了嗎?我也想創建一個名爲「分數」的變量,只要選擇了正確的答案,它就會啓動分數++。打開建議。

+0

你嘗試過什麼嗎? 'String [] questions = new String [] {「Q1」,「Q2」};' –

+0

查看數組和Random對象是否有一個好的起點。如果您在使用這些設備時遇到問題,請發送具體問題的其他問題。一種開始的方法是創建兩個數組,一個用於問題,另一個用於解答兩個數組的相同索引中的問題和答案,並使用Random對象獲取0到數組大小之間的整數,然後然後顯示問題的答案 – NiallMitch14

回答

3

您可以使用Java中可用的Random類生成隨機數。

Random rand = new Random(); 
int randomNum = rand.nextInt((max - min) + 1) + min; 

最大這裏將是你的問題的數量和你的具體情況,您可以設置最小爲0,你可以使用Swing類來設計自己的GUI。大多數IDE(如NetBeans/Eclipse)都可以在不編寫顯式代碼的情況下創建GUI。現在取決於你的隨機數,你可以訪問question[rand]。你可以實現你的問題和答案爲arraylists並作爲問題[rand]訪問你的問題。您可以使用

Collections.shuffle(answerList);來打亂您的相應答案列表。

編輯:這裏是供您參考的樣本代碼:

進口的java.util.ArrayList; import java.util.Iterator; import java.util.Random; import java.util。集合;

公共類JavaApplication1 {

public static void main(String[] args) { 
    /** * Create ArrayList in ArrayList Object */ 
ArrayList<ArrayList<String>> quesAns = new ArrayList<ArrayList<String>>(); 
ArrayList<String> answer = new ArrayList<String>(); 
ArrayList<String> question = new ArrayList<String>(); 
question.add("what's your fruit?"); 
question.add("what's your shape?"); 
answer.add("apple"); 
answer.add("banana"); 
answer.add("mango"); 
quesAns.add(answer); 
ArrayList<String> answer2 = new ArrayList<String>(); 
answer2.add("circle"); 
answer2.add("rectangle"); 
quesAns.add(answer2); 
Random rand = new Random(); 
numQues = 2 
int randomNum = rand.nextInt(numQues); 
System.out.println (question.get(randomNum)); 
ArrayList<String> answerListShuffle = new ArrayList<String>(); 
answerListShuffle = quesAns.get(randomNum); 
Collections.shuffle(answerListShuffle); 
System.out.println(answerListShuffle); 

} 

}

這僅僅是一個示例實現。你可以組成這些排序列表並添加元素,這取決於元素的數量,這取決於元素的數量。

+0

我看到,我開始創建兩個數組:第一個問題是String []問題,然後第二個數組是爲答案是二維數組。字符串[] []回答。問題是,我不知道我將如何協調或捆綁問題的具體答案,同時讓他們在程序運行時隨機顯示。感謝您的回覆。 –

+0

更好的是,您還可以使用arraylist的數組列表,這樣您的所有問題和相應的答案都可以保持組織並相互綁定。在數組上使用數組列表的好處是可以動態分配大小,並且不會綁定到給定的大小。 –

+0

使用arraylist的arraylist會讓你連結你的問題的答案。我給你一個例子:private static ArrayList > ques_ans = new ArrayList >();這是你的ques_ans結構,現在你可以向這個列表添加問題,這將構成外部列表,並且與它們對應的答案將構成內部列表,每個問題對應一個列表。您可以參考http://examplesofjava.com/arraylist/array-list-in-array-list-example.html –

0

最好使用HashMap。因爲使用HashMap,您可以將問題對象的答案對象映射到一起。我認爲這很容易。