我的问题看似简单,希望我能尽快找到答案
为了总结我的代码,我生成一个介于1和1之间的随机数。 2.根据随机数,我有一个包含questionName
或easy1
(easy2
)的字符串"easy" + randomNum
。我想打印questionName
的内容,但不是字符串,而是获取数组easy1[0]
或easy2[0]
。
public void generateQuestion() {
int min = 1;
int max = 2;
Random r = new Random();
int questionToBeSelected = r.nextInt(max - min + 1) + min;
System.out.println(questionToBeSelected);
String questionName = "easy" + questionToBeSelected;
String[] easy1 = {
"In The Phantom Menace, who are the first two characters on screen?", //Question, obviously
"3", //The array child that is correct
"Padme and Annie", //Answer 1
"Obi-Wan and Qui-Gon Jinn", //Answer 2
"Jar Jar Binks", //Answer 3
"Annie and Obi-Wan" //Answer 4
};
String[] easy2 = {
"Sample question",
"2",
"Sample answer",
"Sample answer",
"Sample answer",
"Sample answer",
};
System.out.println(easy1[0]);
TextView questionPlaceholder = (TextView) findViewById(R.id.game_question);
questionPlaceholder.setText(questionName[0]); // <-------- Problem :(
String alreadyPlayedQuestions;
String questionNumber;
}
由于
答案 0 :(得分:1)
请您查看以下内容:
public void generateQuestion() {
int min = 0;
int max = 1;
Random r = new Random();
int questionToBeSelected = r.nextInt(max - min + 1) + min;
System.out.println(questionToBeSelected);
String[] easy1 = {
"In The Phantom Menace, who are the first two characters on screen?", //Question, obviously
"3", //The array child that is correct
"Padme and Annie", //Answer 1
"Obi-Wan and Qui-Gon Jinn", //Answer 2
"Jar Jar Binks", //Answer 3
"Annie and Obi-Wan" //Answer 4
};
String[] easy2 = {
"Sample question",
"2", // array index that is correct
"Sample answer",
"Wrong answer1",
"Wrong answer2",
"Wrong answer3",
};
String[] questionName = new String[][]{easy1, easy2}[questionToBeSelected];
System.out.println(easy1[0]);
TextView questionPlaceholder = (TextView) findViewById(R.id.game_question);
System.out.println(questionName[0]);
questionPlaceholder.setText(questionName[0]); // <-------- Problem :(
String alreadyPlayedQuestions;
String questionNumber;
}
您的方法几乎没有变化。请告诉我这是你想要达到的目的。
答案 1 :(得分:0)
将两个数组存储在另一个数组中并从中选择一个随机元素会更容易:
int questionToBeSelected = r.nextInt(1);
String[] easy1 = {
"In The Phantom Menace, who are the first two characters on screen?", //Question, obviously
"3", //The array child that is correct
"Padme and Annie", //Answer 1
"Obi-Wan and Qui-Gon Jinn", //Answer 2
"Jar Jar Binks", //Answer 3
"Annie and Obi-Wan" //Answer 4
};
String[] easy2 = {
"Sample question",
"2",
"Sample answer",
"Sample answer",
"Sample answer",
"Sample answer",
};
String[][] easy = {easy1, easy2};
String[] selectedQuetion = easy[questionToBeSelected];