我想创建一个测验应用,例如我有3个问题。我想随机询问他们,并在回答完所有问题后结束游戏,但现在我不能这样做。它显示随机问题,但当前会重复。任何帮助将不胜感激。预先感谢。
这是我的代码:
package com.app.twelveimams;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class Quiz extends AppCompatActivity implements View.OnClickListener {
Button btn_one, btn_two, btn_three, btn_four;
TextView tv_question;
private Questions question = new Questions();
private String answer;
private int questionLength = question.questions.length;
Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
random = new Random();
btn_one = (Button)findViewById(R.id.btn_one);
btn_one.setOnClickListener(this);
btn_two = (Button)findViewById(R.id.btn_two);
btn_two.setOnClickListener(this);
btn_three = (Button)findViewById(R.id.btn_three);
btn_three.setOnClickListener(this);
btn_four = (Button)findViewById(R.id.btn_four);
btn_four.setOnClickListener(this);
tv_question = (TextView)findViewById(R.id.tv_question);
NextQuestion(random.nextInt(questionLength));
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_one:
if(btn_one.getText() == answer){
Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}
break;
case R.id.btn_two:
if(btn_two.getText() == answer){
Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}
break;
case R.id.btn_three:
if(btn_three.getText() == answer){
Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}
break;
case R.id.btn_four:
if(btn_four.getText() == answer){
Toast.makeText(Quiz.this, "Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
Toast.makeText(Quiz.this, "Incorrect", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}
break;
}
}
private void GameOver(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Quiz.this);
alertDialogBuilder
.setMessage("Game Over")
.setCancelable(false)
.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
alertDialogBuilder.show();
}
private void NextQuestion(int num){
tv_question.setText(question.getQuestion(num));
btn_one.setText(question.getchoice1(num));
btn_two.setText(question.getchoice2(num));
btn_three.setText(question.getchoice3(num));
btn_four.setText(question.getchoice4(num));
answer = question.getCorrectAnswer(num);
}
}
答案 0 :(得分:0)
您可以使用Collections.shuffle(List<?> list)
对问题列表进行音级化。
package com.kundan.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Question {
public static void main(String[] args) {
ArrayList<String> questionList = new ArrayList<String>();
questionList.addAll(Arrays.asList("Question 1", "Question 2", "Question 3", "Question 4", "Question 5"));
System.out.println(questionList);
Collections.shuffle(questionList);
System.out.println(questionList);
}
}
请参阅下面的链接。
https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)