Here is my code and it works perfectly fine.
import java.util.Random;
class apples
{
public static void main(String args[])
{
Random rand = new Random();
int frequency[] = new int[7];
for(int roll = 1;roll < 1000;roll++){
++frequency[1+rand.nextInt(6)];
}
System.out.println("Face\tFrequency");
for(int face = 1;face < frequency.length;face++){
System.out.println(face + "\t" + frequency[face]);
}
}
}
I do not understand this line of code
++frequency[1+rand.nextInt(6)];
When I removed the "++" operator, it couldn't be compiled. I know that it will add 1 to the randon numbers generated from 0 to 5 but why there is a "++" in front of frequency ? Why is it neccessary to put the "++" operator there ?