Note that there are some explanatory texts on larger screens.

plurals
  1. POprint random letters-LinkedList
    primarykey
    data
    text
    <p>In this program i created a <code>LinkedList('lettersLeft')</code> in which I add <code>Letters</code>('Letters' is a class).Each letter has 3 values :1)which is that letter(ie 'A'),2)the times it appears in the game(ie '9'),3)the points you get (ie '1').Also, with <code>getNextLetter()</code> i get the next random letter from the bag. I want to make a for loop that will print me 2 random letters from the bag with their values(ie "A,9,1 D,4,2"). This is my code(if you dont understand something ask) </p> <p><strong>Letters_bag</strong>:</p> <pre><code>public class Letters_bag { public static final Letters A = new Letters('a', 9, 1); public static final Letters B = new Letters('b', 2, 3); public static final Letters C = new Letters('c', 2, 3); public static final Letters D = new Letters('d', 4, 2); public static final Letters E = new Letters('e', 12, 1); public static final Letters F = new Letters('f', 2, 4); public static final Letters[] allLetters = new Letters[] { Letters_bag.A, Letters_bag.B, Letters_bag.C, Letters_bag.D, Letters_bag.E, Letters_bag.F, }; LinkedList&lt;Letters&gt; lettersLeft = new LinkedList(); public Letters_bag() { // add all the letters addLetter(A); addLetter(B); addLetter(C); addLetter(D); addLetter(E); addLetter(F); } // helper method to add the letters private void addLetter(Letters sl) { for (int i=0;i&lt;sl.getCount();i++) { this.lettersLeft.add(sl); } } /** *Returns the next random letter from the bag. */ Letters getNextLetter() { // shuffle those letters Collections.shuffle(lettersLeft); // return a random letter return lettersLeft.removeFirst(); } } </code></pre> <p><strong>Letters</strong>:</p> <pre><code>public class Letters { private char value; private int count; private int points; public Letters(char value, int count, int points) { this.value = value; this.count = count; this.points = points; } public char getValue() { return value; } public int getCount() { return count; } public int getPoints() { return points; } </code></pre>
    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.
    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