2017-07-26 6 views
0

Ich baue ein Spiel, das eine Frage mit 4 Multiple-Choice-Antworten (in android Studio) hat. Ich werde drei falsche Antworten und eine korrekte Antwort für alle 45 Fragen in meiner App generieren. Ich habe eine separate Methode, die die Frage zufällig generiert (45 verschiedene Fragen). Ich bin mir nicht sicher, wie ich das machen soll.Wie erstelle ich zufällige Strings für ein Multiple-Choice-Spiel

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

Da die möglichen Antworten glaubwürdig sein müssen. Sie können nicht wirklich eine zufällige Zeichenfolge haben. Der beste Ansatz wäre wahrscheinlich, einige zufällige Wörter/Namen aus einer Wortbank auszuwählen. Es ist die gleiche Idee, wie Sie Ihre Frage auswählen. – litelite

+0

Willst du zufällig-zufällig (zB George Taft) oder willst du eine Namensbank mit einem gültigen (wer ist 44.: Obama, Trump, Bush, Clinton) verwenden? – Robert

+2

Verwenden Sie eine Datenbank. Verknüpfen Sie die Fragen und Antworten mit einer ID und markieren Sie die richtige mit einer ganzen Zahl. Die falschen können eine variable Zahl sein, um die Dinge ein wenig zu verwirren. Verwenden Sie '... ORDER BY RANDOM', um die Antworten unsortiert auszuwählen. –

Antwort

0

Der beste Weg ist meiner Meinung nach eine Klasse für Ihre Fragen. Haben Sie eine Klasse für Ihre Antworten. Erstellen Sie in Ihrer Fragenklasse Setter und Getter sowie einen Konstruktor, um die Frage zu einer Liste hinzuzufügen.

In Ihrer Antwortklasse haben Sie Setter und Getter für 3 falsche Antworten und eine für die richtige Antwort. In Ihrem Konstruktor, haben Sie 4 Antworten, von denen eine die richtige Antwort ist. Sie können es so einrichten, dass der erste String im Konstruktor immer die richtige Antwort ist. Von dort fügen Sie das in eine Liste hinzu.

Sie werden zwei Listen haben. Eins mit Fragen. Eins mit Antworten.

Wählen Sie eine beliebige Zahl aus. Wenn diese Zahl 1 ist, GET die Frage an Position 1 in der Fragenliste und erhalten Sie die Antworten auch an Position 1. Da Sie wissen, dass sich die CORRECT-Antwort an der ersten Position des Konstruktors befindet, können Sie diese Antwort mit der Methode answer.getCorrectAnswer() oder was auch immer aus der Liste abrufen.

Hoffentlich macht das Sinn. Es sollte in der Lage sein, von dort aus zu arbeiten.

0

Welche Art von Antwort suchen Sie? Wenn die Fragen arithmetisch sind, können Sie Zufallszahlen für die falschen Antworten generieren (indem Sie nach einem Duplikat der richtigen Antwort suchen). Wenn die Antworten Jahre sind: "Welches Jahr hat ...", dann generiere ein Jahr in einem vernünftigen Bereich und überprüfe erneut, dass es nicht der richtigen Antwort entspricht.

Wenn die Antworten komplexer sind, ist es wahrscheinlich am besten, die falschen Antworten mit den richtigen Antworten zu verwenden. Die Klasse Question enthält vier Zeichenfolgen: rightAnswer und ein Array von drei falschen Antworten: wrongAnswer[0] ... wrongAnswer[2]. Ihre Frage Bank ist ein Array von Question und jede Frage enthält ihre richtige Antwort und drei falsche Antworten.

Wenn die Fragen ähnlich sind, "Welcher Präsident ...", dann können Sie die richtige Antwort von einer Frage als falsche Antwort für verschiedene, aber ähnliche Fragen verwenden.

Verwandte Themen