Did I use arrays right in my Java code?

My program is supposed to roll to determine how many time the number 2comes up when an 11 sided (numbers 2 to 12) and two six sided dice (numbers 1 to 6 on each die. the outcome of each die is added together) is rolled 1000 times. The program should output how many times the 11-sided and the 6-sided dice resulted in the number two after 1000 rolls.

Can someone tell me if I carried out the arrays properly throughout my code? thanks

CODE:

import java.io.*;

public class arrayAssignment

{

public static void main (String [] args) throws IOException

{

BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed

int rollSix [] = new int [1000];

int rollEleven [] = new int [1000];

System.out.println("Two six sided dice: ");

System.out.println("After 1000 rolls, the number 2 came up " + rollDiceA (rollSix) + " times.");

System.out.println("~~~~~~~~~~~~~~~~~~~~…

System.out.println("Eleven sided die: ");

System.out.println("After 1000 rolls, the number 2 came up " + rollDiceB (rollEleven) + " times.");

}

public static int rollDiceA (int rollDiceA []) throws IOException

{

int rollNum = 0;

for (int i = 0; i < 1000; i++)

{

int resultA = (int)(Math.random()*6) + 1;

int resultB = (int)(Math.random()*6) + 1;

int sum = resultA + resultB;

if (sum == 2)

{

rollNum++;

}

}

return rollNum;

}

Re: Did I use arrays right in my Java code?

First, you should put your formatted code between code tags .

Now:

- The array that you pass to the rollDiceA method is not initialized to anything.
- And you never reference the array iinternal to the method.
- You didn't include the rollDiceB method

Having said that, I don't believe you need arrays. You're just summing the times when you roll a two and then returning the number. Arrays don't appear to play any part in that.

Regards,

Jim

Re: Did I use arrays right in my Java code?

thanks for the help!

oops i forgot to paste the third method. this is it:

import java.io.*;

public class arrayAssignment

{

public static void main (String [] args)

{

int rollSix [] = new int [1000];

int rollEleven [] = new int [1000];

System.out.println("Two six sided dice: ");

System.out.println("After 1000 rolls, the number 2 came up " + rollDiceA (rollSix) + " times.");

System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~");

System.out.println("Eleven sided die: ");

System.out.println("After 1000 rolls, the number 2 came up " + rollDiceB (rollEleven) + " times.");

}

public static int rollDiceA (int rollDiceA [])

{

int rollNum = 0;

for (int i = 0; i < 1000; i++)

{

int resultA = (int)(Math.random()*6) + 1;

int resultB = (int)(Math.random()*6) + 1;

int sum = resultA + resultB;

if (sum == 2)

{

rollNum++;

}

}

return rollNum;

}

public static int rollDiceB (int rollDiceB [])

{

int rollNum = 0;

for (int i = 0; i < 1000; i++)

{

int result = (int)(Math.random()*12) + 2;

if (result == 2)

{

rollNum++;

}

}

return rollNum;

}

}

The assignment the teacher gave to us is asking for us to use arrays, is it possible to incorporate them into my program? Thanks :)

Re: Did I use arrays right in my Java code?

Well, you don't need to pass in any arrays. Just record each toss into the array and then return the arrays. Then you could then iterate thru them and count the twos.

Example:

Code:

`int [] twoDice = new int[1000];`

twoDice[] = rollTwoDice();

// Tally the results

public int[] rollTwoDice() {

int[] toss = new int[1000];

// processing

return toss;

}

There are certainly better ways to do it, even with arrays. But this should be a reasonable start.

Regards,

Jim

Re: Did I use arrays right in my Java code?

Thanks Jim!

Just to clarify, would this mean I would remove the arrays I wrote at the beginning of my program and would this replace my (Math.random) lines as well as the counter? Sorry for all the questions, I am very new to Java. Thanks.

Re: Did I use arrays right in my Java code?

Re: Did I use arrays right in my Java code?

Before you start coding you should apply a bit of (simple) math: for the 11 sided die the probability of side 2 is 1/11 (unless it has been fiddled with) but for the two 6 sided dice the probability for two 1s is 1/6*1/6 == 1/36; so for 1000 rolls the probabilities/totals are 1000/11 and 1000/36; and b.t.w. you don't need arrays.

kind regards,

Jos

Re: Did I use arrays right in my Java code?

Quote:

Originally Posted by

**JosAH** ... and b.t.w. you don't need arrays.

kind regards,

Jos

Yeah, that's what I said. But the prof supposedly want's his students to do so.

Regards,

Jim