我是Android App开发的初学者。下面的代码是一个测验应用程序,我希望它循环随机问题,不要重复问题,我试图使用flag2随机生成问题,但我收到编译错误,任何人都可以帮我解决这个问题。我也是Java的初学者。
TextView tv;
Button btn1;
RadioButton rb1,rb2,rb3;
RadioGroup rg;
String Questions[]={"What is 1+1?","Capital of USA?","What is 2+2","Echo with Laughter","Warg"};
String opt[]={"2","3","4", "New York","Washington DC","Maryland", "5","4","6","Stairway to Heaven","Hotel California","Highway to hell","Jon","Bran","Dario" };
String ans[]={"2","Washington DC","4","Stairway to heaven","Bran"};
int flag=0;
public static int correct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv=(TextView)findViewById(R.id.textView2);
btn1=(Button)findViewById(R.id.button2);
rg=(RadioGroup)findViewById(R.id.radioGroup);
rb1=(RadioButton)findViewById(R.id.radioButton);
rb2=(RadioButton)findViewById(R.id.radioButton2);
rb3=(RadioButton)findViewById(R.id.radioButton3);
tv.setText(Questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(ans[flag])) {
correct++;
}
else {
Intent in = new Intent(getApplicationContext(),Token.class);
startActivity(in);
}
flag++;
if (flag < Questions.length) {
tv.setText(Questions[flag]);
rb1.setText(opt[flag * 3]);
rb2.setText(opt[(flag * 3)+1]);
rb3.setText(opt[(flag * 3)+2]);
}
else {
Intent in = new Intent(getApplicationContext(),Token.class);
startActivity(in);
}
}
答案 0 :(得分:1)
使用java.lang.Math.random()。 它将返回从0.0到0.1的值,将这些值转换为整数&amp;得到整数的问题
答案 1 :(得分:1)
生成从0到最大问题范围内的随机数,您可以像下面这样做:
int numberOfQuestion = 5;
Random rn = new Random();
randomNum = rn.nextInt() % numberOfQuestion; // random number from 0 to MaxRange - 1
您可以创建一个类,而不是使用三个数组:
class Quiz{
String question;
String answer;
String[] options;
boolean asked;
}
每次询问特定问题时,只需将标志asked
设为真,然后在询问问题之前,检查是否有问题,如果没有,则只显示问题。
答案 2 :(得分:1)
三弦阵列实际上没用。你应该使用object ArrayList。
public class QuestionObject{
int id;
String question;
String [] options;
String answer;
--you should implement getter and setter--
public int getId(){
return this.id;
}
public void setId(int id){
this.id= id;
}
public String getQuestion(){
return this.question;
}
public void setQuesion(String question){
this.question = question;
}
public String[] getOptions()
{
return options;
}
public void setOptions(String[] options)
{
this.options= options;
}
public int getOptionsElement(int location)
{
return options[location];
}
public void setOptionsElement(int value, int location)
{
options[location] = value;
}
public String getAnswer(){
return this.answer;
}
public void setAnswer(String answer){
this.answer= answer;
}
}
你应该像
一样使用它ArrayList<QuestionObject> questionObject = new ArrayList<QuestionObject>();
ArrayList<QuestionObject> answeredQuestion = new ArrayList<QuestionObject>();
不要忘记用问题选项和aswers填充questionObject。
之后你的逻辑必须实现。你可以在问题显示时删除问题的id并删除列表。也许你可以把删除的问题推到另一个Arraylist。
//take the diplaying question this id can be your random number
int id = questionObject.getId(); //or int id = yourRandomNumber;
//store the another arraylist this question
answeredQuestion.add(questionObject.get(id));
//remove it from list than never show again
questionObject.remove(id);
我认为这可能对你有所帮助。
答案 3 :(得分:0)
修改强>
这是因为虽然无限,新的声明为Lists:
Random random = new Random();
String rightAnswer=null;
List<String> questions=new ArrayList<String>(); // list of questions
List<String> answers=new ArrayList<String>(); // list of answers
String[] ques={"What is 1+1?","Capital of USA?","What is 2+2","Echo with Laughter","Warg"};
questions.addAll(Arrays.asList(ques));
String[] ans={"2","Washington DC","4","Stairway to heaven","Bran"};
answers.addAll(Arrays.asList(ans));
ArrayList<String[]> options = new ArrayList<String[]>(); // list of arrays that holds options
String[] opt1={"2","3","4"};
String[] opt2={"New York","Washington DC","Maryland"};
String[] opt3={"5","4","6"};
String[] opt4={"Stairway to Heaven","Hotel California","Highway to hell"};
String[] opt5={"Jon","Bran","Dario" };
options.add(opt1);
options.add(opt2);
options.add(opt3);
options.add(opt4);
options.add(opt5);
在你想要生成问题的地方,你可以编写这段代码,我在Button的onClick()中假设:
int questionNumber;
if(questions.size()>0) // only run if list contains something
{
questionNumber = random.nextInt(questions.size()); // generate random question from current list
String[] currentOptions=options.get(questionNumber);
tv.setText(questions[questionNumber]);
rb1.setText(currentOptions[0]);
rb2.setText(currentOptions[1]);
rb3.setText(currentOptions[2]);
rightAnswer=answers.get(questionNumber); // hold right answer in this variable
questions.remove(questionNumber); // remove question which is asked
answers.remove(questionNumber); // remove answer which is asked
options.remove(questionNumber); // remove options that are showed
}
else
{
tv.setText("No questions remaining");
}
注意:声明部分必须在生成随机问题的函数之外,否则会失败。