Jump to content
MakeWebGames

Recommended Posts

Posted

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( );
}
}
}
  • 6 months later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...