【发布时间】:2017-07-15 16:17:55
【问题描述】:
我在另一个 java 类中创建了三个数组:问题数组、选择数组、答案数组。现在我希望这些问题在按下选择(错误或错误)时随机随机播放 我的代码首先是 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;
导入 java.util.ArrayList; 导入 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;
}
}
【问题讨论】:
-
这对我没有任何帮助,先生,请告诉我在我的代码中应该写什么!
标签: java android arrays shuffle