Announcement

Collapse
No announcement yet.

2D-Arrays in Java

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2D-Arrays in Java

    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...

    Code:
    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
    Re: 2D-Arrays in Java

    ah, 2d arrays, havent used them in awhile...

    i just use 2 1d arrays, way less complicated :P

    but here is what i remember:

    The .length command does not work very well for 2d arrays.

    2d Arrays can't be referred to as a 1d array, in other words u cant say arrayName[6], its more complicated than that but I forget, so try using google :P

    Comment

    Working...
    X