我正在尝试将随机问题生成到测验中。目前一切都很好,但问题在重复,你怎么能阻止他们重复?我已经阅读了几篇文章,但我还不太了解如何实现代码。
public class CplusQuiz extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
TextView score, question;
private Questions mQuestions = new Questions();
private String mAnswer;
private int mScore = 0;
private int mQuestionLength = mQuestions.mQuestions.length;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cplus_quiz);
r = new Random();
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Nerd Level: " + mScore);
updateQuestion(r.nextInt(mQuestionLength));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer1.getText() == mAnswer) {
mScore++;
score.setText("Score: "+ mScore);
updateQuestion(r.nextInt(mQuestionLength));
}
else {
gameOver();
}
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer2.getText() == mAnswer) {
mScore++;
score.setText("Score: "+ mScore);
updateQuestion(r.nextInt(mQuestionLength));
}
else {
gameOver();
}
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer3.getText() == mAnswer) {
mScore++;
score.setText("Score: "+ mScore);
updateQuestion(r.nextInt(mQuestionLength));
}
else {
gameOver();
}
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer4.getText() == mAnswer) {
mScore++;
score.setText("Score: "+ mScore);
updateQuestion(r.nextInt(mQuestionLength));
}
else {
gameOver();
}
}
});
}
private void updateQuestion(int num) {
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
answer3.setText(mQuestions.getChoice3(num));
answer4.setText(mQuestions.getChoice4(num));
mAnswer = mQuestions.getCorrectAnswer(num);
}
private void gameOver() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CplusQuiz.this);
alertDialogBuilder
.setMessage("Epic Fail... Your nerd level is " + mScore + " ")
.setCancelable(false)
.setPositiveButton("Start Over",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(getApplicationContext(), CplusQuiz.class));
finish();
}
})
.setNegativeButton("EXIT TO MAIN",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
Questions.java文件
package com.example.max.quiz;
/ ** *创建于2017年4月24日的最高价格。 * / 公共课问题{
public String mQuestions[] = {
"WHO IS THE FASTEST OF THESE VIDEO GAME CHARACTERS?",
"IN THE GAME HALO, WHAT IS THE NAME OF MASTER CHIEF'S AI SIDEKICK?",
"WHICH BAD GUY WAS INTRODUCED IN SUPER MARIO BROTHERS 2?",
"WHAT VIDEO GAME CONSOLE HAS THE HIGHEST NUMBER OF VIDEO GAME CONSOLE SALES OF ALL TIME?",
"WHICH OF THESE DO YOU NOT DO IN WE LOVE KATAMARI, THE SEQUEL TO KATAMARI DAMACY?",
"WHICH OF THESE BANDS IS NOT FEATURED IN GUITAR HERO III: LEGENDS OF ROCK?",
"HOW MANY UNLOCKABLE CHARACTERS CAN BE FOUND IN SUPER SMASH BROTHERS?",
"WHAT WAS NINTENDOS FIRST TRY AT AN ARCADE GAME?",
"WHICH DOES NOT HAVE WIFI?",
"WHAT VIDEO GAME CONSOLE HAS THE HIGHEST NUMBER OF VIDEO GAME CONSOLE SALES OF ALL TIME?",
};
private String mChoices[][] = {
{"Mario", "Sonic", "Donkey Kong", "The Paperboy"},
{"Cortana", "Arbiter", "343 Guilty Spark", "HAL"},
{"Koopa troopa", "Lakitu", "Shy Guy", "Goomba"},
{"Xbox 360", "Nintendo 64", "Wii", "PlayStation 2"},
{"roll around under water", "roll around while on fire", "roll around on the moon", "roll around a sumo wrestler"},
{"Metallica", "Weezer", "Iron Maiden", "Lynyrd Skynyrd"},
{"1", "2", "3", "4"},
{"Super Mario Brothers", "Donkey Kong Jr.", "Donkey Kong", "Final Fantasy"},
{"Mario Kart DS", "Diddy Kong Racing DS", "Tony Hawk's American Sk8land", "Super Mario 64 DS"},
{"Xbox 360", "Nintendo 64", "Wii", "PlayStation 2"},
};
private String mCorrectAnswers[] = {"Sonic", "Cortana", "Shy Guy", "PlayStation 2","roll around on the moon", "Iron Maiden", "4", "Donkey Kong", "Super Mario 64 DS", "PlayStation 2" };
public String getQuestion(int a) {
String question = mQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice = mChoices[a][0];
return choice;
}
public String getChoice2(int a) {
String choice = mChoices[a][1];
return choice;
}
public String getChoice3(int a) {
String choice = mChoices[a][2];
return choice;
}
public String getChoice4(int a) {
String choice = mChoices[a][3];
return choice;
}
public String getCorrectAnswer (int a) {
String answer = mCorrectAnswers [a];
return answer;
}
}
答案 0 :(得分:0)
这是JavaScript中的解决方案 - 您可以遵循逻辑并希望将概念应用于Java项目。这个Plunkr https://plnkr.co/edit/KcHh63ou25LZDaZ79iwI?p=preview
中有一个正在运行的演示基本上我们正在做的是创建所有可能问题的初始数组,以及我们将在测验中包含的一系列问题。我们决定要包含多少个问题,在此示例中为5,然后循环该次数。
在每次迭代中,我们得到包含所有可能问题的数组的当前长度,并选择0和数组中剩余的问题数之间的随机数,然后我们将该索引值处的问题推送到我们的数组测验问题的问题,并从我们的一系列可能的问题中提出问题,因此不会再次被选中。
然后我们再次迭代,现在源问题数组是一个更短的,我们得到它的新长度,选择一个新的随机数并抓住那个问题,将它添加到我们的测验数组中,将其切片出来问题数组。
泡沫,冲洗,重复。
questionsSource = [{
"question": "What is your favourite colour?",
"answer": "blue",
}, {
"question": "What is the average airspeed of a coconut laden swallow?",
"answer": "African or European Swallow?",
}, {
"question": "Question 3?",
"answer": "Answer 3",
}, {
"question": "Question 4?",
"answer": "Answer 4",
}, {
"question": "Question 5?",
"answer": "Answer 5",
}, {
"question": "Question 6?",
"answer": "Answer 6",
}, {
"question": "Question 7?",
"answer": "Answer 7",
}, {
"question": "Question 8?",
"answer": "Answer 8",
}]
questionsLeft = questionsSource;
questionsPicked = [];
questionsInQuiz = 5; // how many questions you want to look up
for (i = 0; i < questionsInQuiz; i++) {
questionsLeftInArray = this.questionsLeft.length;
randNumber = Math.floor(Math.random() * questionsLeftInArray);
// pick a random number between 0 and the number of questions left in the array.
this.questionsPicked.push(questionsLeft[randNumber]);
// add the randomly selected question to the questionsPicked array.
this.questionsLeft.splice(randNumber, 1);
// remove that question from the remaining questions so it can't be picked again.
}
console.log("QuestionsSource", questionsSource);
console.log("questionsPicked", questionsPicked);
output = JSON.stringify(questionsPicked, null, 4)
document.write(output);