Click to See Complete Forum and Search --> : sorting data


Static42
09-06-2008, 04:17 PM
public class DiceRoll
{
public static void main(String[] args)
{

int diceArray[] = new int[15000];

for (int i = 0; i < 15000; i++)
{
diceArray[i] = 1 + (int) (Math.random() * 7);

}
}
}


That code will output a random number between 1 and 6. After the for loop, I want the program to tell me how many times each number appeared. So for example, let's say out of the 15000 times, the number 8 appeared 400 times. I want the script to calculate that and present me with that number.


for (int pass = 1; pass < diceArray.length; pass++)
{
//code
}


That for loop will go through the entire array. I just need help with finding how many times each number appeared.

kurent
09-07-2008, 12:07 PM
You don't need another loop, just do it all at once.


public class DiceRoll
{
public static void main(String[] args)
{
int diceArray[] = new int[15000];
int resultArray[] = new int[7];

for (int i = 0; i < 15000; i++)
{
diceArray[i] = 1 + (int) (Math.random() * 7);
switch(diceArray[i])
{
case 1: resultArray[1]++;
case 2: resultArray[2]++;
case 3: resultArray[3]++;
case 4: resultArray[4]++;
case 5: resultArray[5]++;
case 6: resultArray[6]++;
default: System.out.println("This is not suppose to happen, computer revolution!");
}
}

System.out.println("Number 1 was rolled " + resultArray[1] + " times");
System.out.println("Number 2 was rolled " + resultArray[2] + " times");
System.out.println("Number 3 was rolled " + resultArray[3] + " times");
System.out.println("Number 4 was rolled " + resultArray[4] + " times");
System.out.println("Number 5 was rolled " + resultArray[5] + " times");
System.out.println("Number 6 was rolled " + resultArray[6] + " times");
}
}