Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>To convert from boolean to int:</strong></p> <p>In general, assuming you have some boolean <code>b</code>, I would use this:</p> <pre><code>int x = b? 1:0; // If b is true, x is now 1; if b is false, x is now 0. </code></pre> <p>This uses the <a href="http://en.wikipedia.org/wiki/?%3a#Java" rel="nofollow">ternary operator</a>.</p> <p>In your case, however, this construction is unnecessary. If I understand correctly, you want index <code>i</code> of <code>counts</code> to hold the number of times that the dice summed to that index. To do that:</p> <pre><code>int sumOfDice = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1); counts[sumOfDice]++; </code></pre> <p><strong>To return an array:</strong></p> <p>Returning an array is much like returning an int. Just change the method declaration for 'input()' to</p> <pre><code>public static int[] input () throws IOException { </code></pre> <p>and the return statement to </p> <pre><code>return counts; </code></pre> <p><strong>To print an array:</strong></p> <p>Import <code>java.util.Arrays</code> and call <code>Arrays.toString(yourArrayHere)</code>.</p> <p>The complete program now looks like:</p> <pre><code>import java.io.*; import java.util.Arrays; public class dont { public static void main(String[] args) throws Exception { int[] counts = input(); System.out.println("The number of times it rolls 4 on two 6 sided dice :" + counts[4]); System.out.println("The number of times each number was the sum:" + Arrays.toString(counts); } public static int[] input () throws IOException { BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); System.out.println("Hello and welcome to the program"); System.out.println("In this program two six sided dices will be rolled and one eleven sided dice will be rolled (1000 times each"); // We don't actually roll any eleven-sided dice, so I'm not sure why this is here? int[] counts = new int[13]; System.out.println("The dices will be rolled to determine the odds of how many times the roll 2 comes up on both dies(Press any key to con't) "); myInput.readLine(); for (int i = 0; i &lt; 1000; i++) { int sumOfDice = (int)(Math.random ()*6+1) + (int)(Math.random ()*6+1); counts[sumOfDice]++; } // return array to main return counts; } } </code></pre>
    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.
 

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