Results 1 to 7 of 7

Thread: 2D Arrays

  1. #1
    Major90 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    print out the sum of 2 randomly generated dice
    Where are you printing the sum?
    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.

  3. #3
    Major90 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    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 03:46 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. #5
    Major90 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    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. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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 02:10 PM.

Similar Threads

  1. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 02:34 AM
  2. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 02:24 AM
  3. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 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
  •