Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, instead of making it a <code>String[][]</code> array, you should make it a <code>Drink[]</code> array (where <code>Drink</code> is a class that you have defined and which has a <code>String</code> name, and a <code>float</code> price. This will help you use the info, because you won't have to constantly worry about parsing the price String to a double.</p> <p>Here is a pseudocode solution if the array is sorted by price (from lowest to highest):</p> <ol> <li>First find the most expensive drink that you can still buy. You could binary search for it, but a simpler solution would be to go from index 0 to Test.length-1 checking whether or not the drink would be buyable. If not, you would stop and store the index of the last buyable drink. If it were buyable, you would continue.</li> <li>Then, you would generate a random <code>int</code> from 0 to the <code>maxIndex</code> (inclusive), and output the Drink.</li> </ol> <p>For example,</p> <p><code>(int)(Math.random()*(maxIndex+1))</code> would get you the random integer.</p> <p><strong>EDIT</strong></p> <p>Since the array is not necessarily sorted, you can sort it. In order to do this, use <code>java.util.Arrays.sort(Object[] o, Comparator c)</code> to sort it.</p> <p>Input your <code>Drink[]</code> as the first parameter. And your <code>DrinkComparator</code> as your second. This will <code>quicksort</code> it for you.</p> <p>Assuming that your <code>Drink</code> class is defined as follows:</p> <pre><code>public class Drink { String name; double price; // You could also use floats public Drink(String n, double p) { price = p; name = n; } } </code></pre> <p>You can make your <code>DrinkComparator</code> class like this.</p> <p><a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html" rel="nofollow">http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html</a></p> <pre><code> class DrinkComparator implements Comparator { public int compare(Object o1, Object o2) { if(o1.price &lt; o2.price) { return 1; } else if(o1.price == o2.price) { // Disregarding float imprecision return 0; } else { // Not necessary, but here for the sake of readability. return -1; } } public boolean equals(Object o) { // I don't think you will be using this method. return true; // If you run into problems, tell me. } } } </code></pre> <p>Then you would sort like this:</p> <p><code>Arrays.sort(drinks, new DrinkComparator());</code></p> <p><code>drinks</code> would be your <code>Drink[]</code> of course.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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