Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot make this code work properly
    primarykey
    data
    text
    <p>How do I make it show answers randomly on one of 4 lines, without having any duplicates?</p> <p>My current code:</p> <pre><code>TextView question; private int qType = -1; private int asked = 0; private void QBegin() { /* * Gets a 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]) &amp;&amp; asked &gt;= types.length+1){ asked = 0; answerCounter.setText("THE END"); } else if (StringList.contains(types[qType]) &amp;&amp; asked &lt; 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&lt;Character&gt; characters = new ArrayList&lt;Character&gt;(); 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 &gt; 3 ? (randomValue+0)-4 : randomValue+0]); answer2.setText(answers_list[Type][randomValue+2 &gt; 3 ? (randomValue+1)-3 : randomValue+1]); answer3.setText(answers_list[Type][randomValue+3 &gt; 3 ? (randomValue+2)-2 : randomValue+2]); answer4.setText(answers_list[Type][randomValue+0 &gt; 3 ? (randomValue+3)-4 : randomValue+3]); /*for (int rows = 0; rows &lt; answer&amp;*list.length; rows++){ for (int cols = 0; cols &lt; answers_list[rows].length; cols++){ } }*/ } catch(Exception ex){ answer1.setText("Error "+ex); } } </code></pre> <p>Code that's responsible for picking &amp; adding questions on one of 4 lines (randomly)</p> <pre><code> answer1.setText(answers_list[Type][randomValue+1 &gt; 3 ? (randomValue+0)-4 : randomValue+0]); answer2.setText(answers_list[Type][randomValue+2 &gt; 3 ? (randomValue+1)-3 : randomValue+1]); answer3.setText(answers_list[Type][randomValue+3 &gt; 3 ? (randomValue+2)-2 : randomValue+2]); answer4.setText(answers_list[Type][randomValue+0 &gt; 3 ? (randomValue+3)-4 : randomValue+3]); </code></pre> <p>At the moment, I'm having duplicates with answer1 and answer4. Please Help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload