2017-07-26 61 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的数组,每个问题将包含正确答案和三个错误答案。

如果问题是相似的,“哪位总统......”,那么你可以用一个问题的正确答案作为不同但相似问题的错误答案。