java - Cannot make this code work properly -


how make show answers randomly on 1 of 4 lines, without having duplicates?

my current code:

textview question; private int qtype = -1; private int asked = 0; private void qbegin() {     /*      * gets random question     */     question = (textview) findviewbyid(r.id.question);     string[] types = { "q1", "q2", "q3", "q4", "q5"};     random random = new random();     int qtype = random.nextint(types.length);     question.settext(types[qtype]);     asked++;    // stringlist.add(types[qtype]);     getanswers(qtype);  /*   if(stringlist.contains(types[qtype]) && asked >= types.length+1){         asked = 0;         answercounter.settext("the end");     } else if (stringlist.contains(types[qtype]) && asked < types.length+1){         qbegin();     } */ }      public static int random(int range) {     return (int)(java.lang.math.random() * (range+1)); }  public void shuffle(string input){     /*      * unused shuffle method      */     list<character> characters = new arraylist<character>();     for(char c:input.tochararray()){         characters.add(c);     }     stringbuilder output = new stringbuilder(input.length());     while(characters.size()!=0){         int randpicker = (int)(math.random()*characters.size());         output.append(characters.remove(randpicker));     }     system.out.println(output.tostring()); } private void getanswers(int type) {     /*       * getting answers here      */     int randomvalue = random(4);     try {      string answers_list[][] = {             {"answer 1-1", "answer 2-1", "answer 3-1", "answer 4-1"},              {"answer 1-2", "answer 2-2", "answer 3-2", "answer 4-2"},             {"answer 1-3", "answer 2-3", "answer 3-3", "answer 4-3"},             {"answer 1-4", "answer 2-4", "answer 3-4", "answer 4-4"},             {"answer 1-5", "answer 2-5", "answer 3-5", "answer 4-5"}} ;     answer1.settext(answers_list[type][randomvalue+1 > 3 ? (randomvalue+0)-4 : randomvalue+0]);      answer2.settext(answers_list[type][randomvalue+2 > 3 ? (randomvalue+1)-3 : randomvalue+1]);      answer3.settext(answers_list[type][randomvalue+3 > 3 ? (randomvalue+2)-2 : randomvalue+2]);      answer4.settext(answers_list[type][randomvalue+0 > 3 ? (randomvalue+3)-4 : randomvalue+3]);       /*for (int rows = 0; rows < answer&*list.length; rows++){     (int cols = 0; cols < answers_list[rows].length; cols++){          }         }*/     } catch(exception ex){     answer1.settext("error "+ex);     }    } 

code that's responsible picking & adding questions on 1 of 4 lines (randomly)

    answer1.settext(answers_list[type][randomvalue+1 > 3 ? (randomvalue+0)-4 : randomvalue+0]);      answer2.settext(answers_list[type][randomvalue+2 > 3 ? (randomvalue+1)-3 : randomvalue+1]);      answer3.settext(answers_list[type][randomvalue+3 > 3 ? (randomvalue+2)-2 : randomvalue+2]);      answer4.settext(answers_list[type][randomvalue+0 > 3 ? (randomvalue+3)-4 : randomvalue+3]); 

at moment, i'm having duplicates answer1 , answer4. please help.

if understand goal correctly, you're trying randomly shuffle list of questions. rossum gave 1 way , this introduction best known options.

in opinion, simplest approach:

  1. create map sorts key (e.g. treemap <double,string> or <double,question>)
  2. add random key , question value
  3. loop through , print results. foreach loop use sort order

don't worry duplicate random numbers or normalizing it. dups unlikely , if need question #, add counter in loop.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -