2016-03-01 93 views
2

我是Android应用程序开发的初学者。下面的代码是一个测验应用程序,我希望它循环随机问题,不要重复这个问题,我试图用一个flag2来随机生成问题,但我得到编译错误,任何人都可以帮我解决这个问题。我也是Java的初学者。Android - 循环随机阵列,不重复

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

编辑:

这是由于同时进入无限,新声明的清单:

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); 

而且要产生你可以这样写代码的问题,我假设的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"); 
    } 

这是测试,我能得到这些结果: enter image description here

注意:声明部分必须在生成随机问题的函数之外,否则将失败。

+0

这对我有效,但我似乎无法进入下一个问题,它给了我错误“太多的输出”。 – Moushfiq

+0

查看我所做的修改。 – Talha

1

use java.lang.Math.random()。 它将从0.0返回值0.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到TRU时间e,在提出问题之前,只需检查是否询问该问题,如果不是,则只显示问题。

1

三串数组实际上没用。你应该使用对象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); 

我认为这可能会对您有所帮助。