Results 1 to 7 of 7
Thread: 2D Arrays
 11062008, 01:49 AM #1Member
 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( );
}
}
}
 11062008, 03:41 AM #2print out the sum of 2 randomly generated dice
Would this work: System.out.println("sum= " + dice);
What is the code supposed to be doing? Can you add comments to the statements describing what they are doing?
Does the program compile without errors?
Does the program execute without errors?
Is the output not what you want?
Please explain.
 11062008, 04:43 AM #3Member
 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; 11062008 at 04:46 AM.
 11062008, 06:01 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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?
 11062008, 07:23 AM #5Member
 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.
 11062008, 07:41 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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.
 11062008, 03:08 PM #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... * *   * *
Last edited by Norm; 11062008 at 03:10 PM.
Similar Threads

need help with arrays
By Jman in forum New To JavaReplies: 17Last Post: 07212008, 03:34 AM 
Arrays
By bunbun in forum New To JavaReplies: 1Last Post: 04092008, 03:24 AM 
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04082008, 03:45 PM 
2DArrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02072008, 11:08 PM 
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11232007, 08:23 PM
Bookmarks