我在另一個java類中創建了三個數組:Array Array,Choices Array,Answers Array。現在,我希望在按下選擇(錯誤或錯誤)時,這些問題將隨機隨機洗牌。我的代碼首先是主要活動旁邊的QuestionLibrary。將數組中的隨機問題
package amapps.impossiblequiz;
public class QuestionLibrary {
private String mQuestions[] = {
"When was the European Union founded?",
"How many Grad Celsius is one Kelvin?",
"What is THC?",
"How many legs has a spider?",
"How many stars has the European flag?",
"Which is the seventh planet from the sun?",
"What is the chemical formula of salt?",
"Who said: Ich bin ein berliner?",
"To which country belongs Greenland?",
"What is the result of: 2 + 2 *5?",
"How many mountains are higher than 8000 meter/26.246 ft?",
"A famous quote is: to be, or____ to be!",
"What is the name of Stalingrad nowadays?"
};
private String mChoices[][] = {
{"1993", "1986", "1967"},
{"-260", "-272,15", "279,15"},
{"a plant","The active substance of marijuana" , "a spider"},
{"6", "10","8"},
{"12","15","10"},
{"Uranus","Neptune","Saturn"},
{"HCl","NaCl","CO"},
{"John F. Kennedy", "Richard Nixon","James A. Garfield"},
{"Canada","Denmark", "Greenland is an own state?"},
{"12","20","14"},
{"10","12","14"},
{"not","never","now"},
{"Leningrad","Wolgograd","Dimitrijgrad"}
};
private String mCorrectAnswers[] = {"1993", "-272,15", "The active substance of marijuana", "8", "12","Uranus","NaCl","John F. Kennedy","Denmark","12","14","not","Wolgograd"};
public String getQuestion (int a){
String question = mQuestions[a];
return question;
}
public String getChoice1 (int a){
String choice0 = mChoices[a][0];
return choice0;
}
public String getChoice2 (int a) {
String choice1 = mChoices[a][1];
return choice1;
}
public String getChoice3 (int a) {
String choice2 = mChoices [a] [2];
return choice2;
}
public String getCorrectAnswer (int a){
String answer = mCorrectAnswers [a];
return answer;
}
}
如果正確=隨機問題從陣列/如果假太
mButtonChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (mButtonChoice1.getText() == mAnswer) {
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optional...
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();
mScore = 0;
updateScore(mScore);
updateQuestion();
}
}
});
現在,我嘗試了新的代碼洗牌我的數組,但所有的全錯誤的....
包amapps.impossiblequiz;
import java.util.ArrayList; import java.util.List;
公共類問題{
private String question;
private String[] choices;
private String answer;
public Question(String question, String[] choices, String answer) {
super();
this.question = question;
this.choices = choices;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public String[] getChoices() {
return choices;
}
public String getAnswer() {
return answer;
}
}
//create list
List<Question> questions = new ArrayList<Question>();
//add one question
questions.add(
new Question(
"What's you name?",
new String[]{"Foo","Bar","John","Doe"},
"Bar"
)
);
//add another question
questions.add(
new Question(
"What's you name?",
new String[]{"Foo","Bar","John","Doe"},
"Bar"
)
);
//shuffle questions
Collections.shuffle(questions);
public int getlength() {
int length = 13;
return length;
}
}
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – frozen
這沒有幫助我什麼,請告訴我在我的代碼裏寫什麼先生! – ProjectX