patternjavaModerate
Random Q&A system
Viewed 0 times
randomsystemstackoverflow
Problem
Basically, I made a simple app for my Android, where it picks a random question for you, and picks specific answers. Only 1 of the answers is correct, while other's aren't.
Also, after editing the
```
TextView question;
private int qType = -1;
private int asked = 0;
private void QBegin() {
// TODO Auto-generated method stub
question = (TextView) findViewById(R.id.question);
Random random = new Random();
int qType = random.nextInt(5);
switch(qType){
case 0:
question.setText("Question 1");
break;
case 1:
question.setText("Q2");
break;
case 2:
question.setText("Q3");
break;
case 3:
question.setText("Q4");
break;
case 4:
question.setText("Q5");
break;
}
asked++;
//intList.add(qType);
getAnswers(qType);
/*if(intList.contains(qType) && asked <= 5){
QBegin();
} else {
answerCounter.setText("Congratulations!!! Your score : "+correct);
}*/
}
private int answer;
private void getAnswers(int Type) {
Random random = new Random();
// TODO Auto-generated method stub
switch(Type){
case 1:
if(random.nextInt(4) == 0){
answer = 1;
answer1.setText("относительно низкая температура шлаков");
answer2.setText("сложность в управлении");
answer3.setText("малая производительность");
answer4.setText("нету выделения энергии непосредственно в загрузке");
} else if (random.nextInt(4) == 1){
answer = 2;
answer1.setText("сложность в управлении");
answer2.setText("относительно низкая температура шлаков");
answer3.setText("малая производительность");
answer4.setText("нету выделения энергии непосредственно в загрузке");
} else if (random.nextInt(4) == 2){
answer = 3;
answer1.set
Also, after editing the
QBegin method, it started to give me questions but with wrong answers.```
TextView question;
private int qType = -1;
private int asked = 0;
private void QBegin() {
// TODO Auto-generated method stub
question = (TextView) findViewById(R.id.question);
Random random = new Random();
int qType = random.nextInt(5);
switch(qType){
case 0:
question.setText("Question 1");
break;
case 1:
question.setText("Q2");
break;
case 2:
question.setText("Q3");
break;
case 3:
question.setText("Q4");
break;
case 4:
question.setText("Q5");
break;
}
asked++;
//intList.add(qType);
getAnswers(qType);
/*if(intList.contains(qType) && asked <= 5){
QBegin();
} else {
answerCounter.setText("Congratulations!!! Your score : "+correct);
}*/
}
private int answer;
private void getAnswers(int Type) {
Random random = new Random();
// TODO Auto-generated method stub
switch(Type){
case 1:
if(random.nextInt(4) == 0){
answer = 1;
answer1.setText("относительно низкая температура шлаков");
answer2.setText("сложность в управлении");
answer3.setText("малая производительность");
answer4.setText("нету выделения энергии непосредственно в загрузке");
} else if (random.nextInt(4) == 1){
answer = 2;
answer1.setText("сложность в управлении");
answer2.setText("относительно низкая температура шлаков");
answer3.setText("малая производительность");
answer4.setText("нету выделения энергии непосредственно в загрузке");
} else if (random.nextInt(4) == 2){
answer = 3;
answer1.set
Solution
The (or at least "One") obvious approach would be to move most of your data into arrays, and just use the numbers to select values from those arrays. For example:
Most of
Personally, however, I think I'd move the text of the questions and answers out to a text file (or a database) and write the code as a more or less generic "engine" that read and present the questions and answers from the file.
private void QBegin() {
question = (TextView) findViewById(R.id.question);
String[] types = { "Question 1", "Q2", "Q3", "Q4", "Q5"};
Random random = new Random();
int qType = random.nextInt(types.length);
question.setText(types[qType]);
asked++;
getAnswers(qType);
}Most of
getAnswers should end up similar, although I'm afraid I'm lack the ambition to type it all in.Personally, however, I think I'd move the text of the questions and answers out to a text file (or a database) and write the code as a more or less generic "engine" that read and present the questions and answers from the file.
Code Snippets
private void QBegin() {
question = (TextView) findViewById(R.id.question);
String[] types = { "Question 1", "Q2", "Q3", "Q4", "Q5"};
Random random = new Random();
int qType = random.nextInt(types.length);
question.setText(types[qType]);
asked++;
getAnswers(qType);
}Context
StackExchange Code Review Q#5242, answer score: 10
Revisions (0)
No revisions yet.