2017-07-26 60 views
0

我正在構建一個有4個多選答案(在android studio中)的問題的遊戲。我將在我的應用程序中爲所有45個問題生成三個錯誤答案和一個正確答案。我有一個單獨的方法隨機生成問題(45個不同的問題)。我不太確定如何做到這一點。如何爲多選遊戲生成隨機字符串

public class MainActivity extends AppCompatActivity { 

    Button startGame; 
    TextView questionTextView; 
    questions question = new questions(); 
    ArrayList<String> answers; //gets the correct 
    int correctAnswer; 

    public void START (View view) { 

     startGame.setVisibility(View.INVISIBLE); 

    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Random rand = new Random(); 
     startGame = (Button) findViewById(R.id.startButton); 
     questionTextView = (TextView) findViewById(R.id.questionTextview); 
     questionTextView.setText(question.getQuestion()); 


     correctAnswer = rand.nextInt(4); 

     for(int i = 0; i < 4; i++) { 

      if(i == correctAnswer) { 


      } else { 


      } 
     } 
    } 
} 






public class questions { 

    public String [] mquestions = { 


      "Who is the 1st president of the United States?", 
      "Who is the 2nd president of the United States?", 
      "Who is the 3rd president of the United States?", 
      "Who is the 4th president of the United States?", 
      "Who is the 5th president of the United States?", 
      "Who is the 6th president of the United States?", 
      "Who is the 7th president of the United States?", 
      "Who is the 8th president of the United States?", 
      "Who is the 9th president of the United States?", 
      "Who is the 10th president of the United States?", 
      "Who is the 11th president of the United States?", 
      "Who is the 12th president of the United States?", 
      "Who is the 13th president of the United States?", 
      "Who is the 14th president of the United States?", 
      "Who is the 15th president of the United States?", 
      "Who is the 16th president of the United States?", 
      "Who is the 17th president of the United States?", 
      "Who is the 18th president of the United States?", 
      "Who is the 19th president of the United States?", 
      "Who is the 20th president of the United States?", 
      "Who is the 21st president of the United States?", 
      "Who is the 22nd president of the United States?", 
      "Who is the 23rd president of the United States?", 
      "Who is the 24th president of the United States?", 
      "Who is the 25th president of the United States?", 
      "Who is the 26th president of the United States?", 
      "Who is the 27th president of the United States?", 
      "Who is the 28th president of the United States?", 
      "Who is the 29th president of the United States?", 
      "Who is the 30th president of the United States?", 
      "Who is the 31st president of the United States?", 
      "Who is the 32nd president of the United States?", 
      "Who is the 33rd president of the United States?", 
      "Who is the 34th president of the United States?", 
      "Who is the 35th president of the United States?", 
      "Who is the 36th president of the United States?", 
      "Who is the 37th president of the United States?", 
      "Who is the 38th president of the United States?", 
      "Who is the 39th president of the United States?", 
      "Who is the 40th president of the United States?", 
      "Who is the 41st president of the United States?", 
      "Who is the 42nd president of the United States?", 
      "Who is the 43rd president of the United States?", 
      "Who is the 44th president of the United States?", 
      "Who is the 45th president of the United States?", 

    }; 


    public String getQuestion() { 

     String question = ""; 

     Random rand = new Random(); 
     //This randomizes the questions!! 
     int randomNumber = rand.nextInt(mquestions.length); 

     question = mquestions[randomNumber]; 

     return question; 

    } 


} 
+0

由於可能的答案需要可信。你不能真的只有一個隨機的字符串。最好的方法可能是從單詞庫中挑選一些隨機單詞/名稱。這與你如何選擇你的問題是一樣的想法。 – litelite

+0

你想要隨機隨機(即喬治塔夫脫),或者你想要使用一個有效的名字銀行(誰是第44:奧巴馬,特朗普,布什,克林頓)? – Robert

+2

使用數據庫。通過一個id關聯問題和答案並用一個整數標記正確的答案。錯誤的可以是一個可變數字,所以混淆了一下。使用'... ORDER BY RANDOM'來選擇未排序的答案。 –

回答

0

在我看來,最好的方法是爲你的問題提供一個類。爲你的答案准備一堂課。在您的問題類中,創建setter和getters以及構造函數以將問題添加到列表中。

在你的答案類中,有3個錯誤答案和1個正確答案的setter和getter。在你的構造器中,有4個答案,其中一個答案是正確的。你可以設置它,這樣構造函數中的第一個字符串總是正確的答案。從那裏,將其添加到列表中。

您將有兩個列表。 一個有問題。 一個有答案。

隨機選擇一個數字。 如果這個數字是1,那麼在問題列表中的位置1處獲得問題,並在位置1處獲得答案。既然你知道正確的答案是在構造函數的第一個位置,你可以使用answer.getCorrectAnswer()或其他方法從列表中獲得答案。

希望這是有道理的。它應該能夠從那裏得到解決。

0

你在找什麼樣的答案?如果問題是算術問題,那麼您可以爲錯誤的答案生成隨機數(檢查正確答案的副本)。如果答案是幾年:「哪一年......」,然後在合理範圍內生成一年,再次檢查它與正確答案不符。

如果答案比較複雜,那麼最好是在錯誤的答案中加上正確的答案。 Question類將包含四個字符串:rightAnswer和三個錯誤答案的數組:wrongAnswer[0] ... wrongAnswer[2]。您的問題銀行是Question的數組,每個問題將包含正確答案和三個錯誤答案。

如果問題是相似的,「哪位總統......」,那麼你可以用一個問題的正確答案作爲不同但相似問題的錯誤答案。