3 Replies - 1349 Views - Last Post: 08 September 2010 - 05:31 PM Rate Topic: -----

#1 jwehrly2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-September 10

2 dimensional array

Posted 07 September 2010 - 06:00 PM

Hello, I am trying to get a 2 dimensional array to print out correctly. My requirements are that I need the array to loop 64,000 times and have 2 dice and tally the combination of the rolls. Here is my code any help will be appreciated thanks.


package twodarray;

import java.util.Random;
public class TwoDarray {

    
    public static void main(String[] args) {

      
       loopArray();
        
 }


public static void loopArray ()
{
    Random rand = new Random();
    int counter2D[][] = new int [9][9];
    
    for (int i = 0; i <= 64000; i++) {
        counter2D[ rand.nextInt(8) ] [rand.nextInt (8)]++;
       
        System.out.printf ("%20s\n",i);
} 
    
}



This post has been edited by jwehrly2000: 07 September 2010 - 06:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 2 dimensional array

#2 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: 2 dimensional array

Posted 07 September 2010 - 06:08 PM

What problems are you having? What does it print?
Was This Post Helpful? 0
  • +
  • -

#3 jwehrly2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-September 10

Re: 2 dimensional array

Posted 08 September 2010 - 12:22 PM

I am working on a 2 dimensional array to tally the results of dice rolls i.e like 7 | 2. I am using the random scanner but I cannot figure out how to get the values to print out that the random generator gets. Any help would be greatly appreciated.

package twodarray;

import java.util.Random;
public class TwoDarray {

    
    public static void main(String[] args) {

      System.out.printf ("%s%10s\n", "Index","Value");
     

      loopArray();
      printArray();
      
      
 }


public static void loopArray ()
{   
    Random rand = new Random();
    int counter2D[][] = new int [9][9];
     
    for (int i = 1; i <= 64000; i++)
{
      counter2D[ rand.nextInt(8) ][ rand.nextInt(8)] ++;
        
      System.out.printf("%20s | %20s\n", i, counter2D[i]);
}  
}
   
    public static void printArray(int... counter2D)
    {
        for ( int i = 1; i < counter2D.length; i++ )
        {



Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: 2 dimensional array

Posted 08 September 2010 - 05:31 PM

Duplicate topics merged. Please avoid duplicate posting.

The variable name length is the number of arrays in the 2D array. The varName[index].length is the length of the individual arrays. So for example:
for(int i = 0; i < myArr.length; i++){ //foreach array
   for(int j = 0; j < myArr[i].length; i++){ //foreach elem in myArr[i]

   }
}



And just print the given element. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1