1. Member
Join Date
Nov 2008
Posts
3
Rep Power
0

## 2D Arrays

I'm new to Java and I'm trying to get a good grasp on 2D arrays. In this code, I'm trying to print out the sum of 2 randomly generated dice. I'm having a great deal of trouble with it so any guidance would be much appreciated. Here's what I have so far...

public class chance
{
public static void main(String[] args)
{
int dice, dice1, dice2;
dice1 = ((int)(Math.random() * 6)) +1;
dice2 = ((int)(Math.random() * 6)) +1;
dice = dice1 + dice2;

int table[][] = new int[3][3];

int sum = 0;
int i, j;
for (i = 0; i < 3; i++)
{// 3 rows: i = 0,1,2
for (j = 0; j < 3; j++)
{
sum += table[i][j];
}
}

for (i = 0; i < table.length; i++)
{
for (j = 0; j < table[i].length; j++)
System.out.print(table[i][j] + " ");
System.out.println( );
}
}
}

2. print out the sum of 2 randomly generated dice
Where are you printing the sum?
Would this work: System.out.println("sum= " + dice);

Does the program compile without errors?
Does the program execute without errors?
Is the output not what you want?

3. Member
Join Date
Nov 2008
Posts
3
Rep Power
0
What I have compiles without error. I don't know how to make it print out the sum of 2 randomly rolled dice using a 2D array. I know how to do this without using a 2D array, but this will help me learn 2D arrays.

The output for each dice would look like the following after it is executed...

Example: Say the first dice is rolled and it is a 4. The output would be...

*| |*
| |
*| |*

(The vertical lines just show the borders of the 3 x 3 2D Array)
Last edited by Major90; 11-06-2008 at 04:46 AM.

4. Still few confusion is there lol. You wants to add sum to the 2D array, but where. I mean on which index you want to store values?

5. Member
Join Date
Nov 2008
Posts
3
Rep Power
0
This is what I'm basically trying to do.

I'm trying to make a game that simulates the rolling of dice by a single player and determines whether the player wins or loses according to the rules of the casino game known as Chance (you may also now this game by the name of Craps). The player rolls two dice, and the spots on the dice are added together. The player wins on the first roll if the dice show a total of 7 or 11. The player loses if the total is 2, 3, or 12. If another number is thrown on the first roll - 4, 5, 6, 8, 9, or 10 - that number becomes the point, or the goal of subsequent tosses. The player continues to roll the dice until the point or a 7 appears. Throwing a total equal to the point means the player wins; throwing a total of 7 means the player loses.

I'm trying to create this using 2D arrays.

6. Say a user throws two dice, and get values 3 and 7; so the sum/result is 10. Now where/how you going to add that into 2D array. How to indexing with a single value.

7. What are you going to use the array for?
Can you show how you'd use the array using array notation?
For example:
int[][] anArray = ... // define the array

anArray[0][0] = value for first row(0), first column(0)
anArray[0][1] = value for first row(0), second column(1)
....
anArray[2][1] = value for third row(2), second column(1)

What data values would you put in each row?
What data values would you put in each column?
Java Code:
```Example: Say the first dice is rolled and it is a 4. The output would be...

*| |*
| |
*| |*```
Can you explain this diagram you've posted? How does a roll of 4 relate to a 3x3 array with * in the corners?
Last edited by Norm; 11-06-2008 at 03:10 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•