mardi 3 février 2015

Java assign two dimensional array with random

Im working on the Exercises Arrays and ArrayLists form the book Java How to program by Deitel 9th edition. Here's is the code followed by my questions



public static void main(String[] args) {
// TODO code application logic here
Random r = new Random();

int[][] sales = new int[5][4];

// display salesnames
System.out.println("\t\t 1.Tom 2.Eva 3.Jan 3.Roy Total);

// declare ProductCounter
int proCount = 1;

// display array
for (int row = 0; row < sales.length; row++) {
System.out.print("Product " + proCount + "\t");

for (int column = 0; column < sales[row].length; column++) {
sales[row][column] = 0 + r.nextInt(2);
System.out.printf(" %d\t", sales[row][column]);

}
proCount++;
System.out.println();
}

}

}


After the second for loop i fill the array with random numbers 0 or 1 and the result looks like this:



1.Tom 2.Eva 2.Jan 4.Roy Product Totals
Product 1 1 0 1 0
Product 2 0 0 1 1
Product 3 0 1 0 1
Product 4 0 0 0 1
Product 5 0 0 0 0


Question: Each salesperson passes in 0-5 sales slips per day


Problem: This random only randoms one time per product. How do I code so it randoms between 0-5 so that could also mean that it could be 3 of product number2 because now it only decides if one product is sold one time or not.





Aucun commentaire:

Enregistrer un commentaire