import java.util.Random;
public class DemoArrayElement {
public static void main(String arg[]) {
Random rand = new Random();
int[] freq = new int[7];
for (int roll = 1; roll < 10; roll++) {
++freq[1 + rand.nextInt(6)];
}
System.out.println("FACE\tFREQUENCY");
for (int face = 1; face < freq.length; face++) {
System.out.println(face + "\t\t" + freq[face]);
}
}
}
有人可以請解釋我這++freq[1+rand.nextInt(6)];
代碼行。我們如何使用數組元素作爲java中的計數器?
但是,當我運行這個程序頻率[face]工作作爲一個計數器... – Prakash
重要的是要注意,它不會碰到'freq'的第0個元素,因爲'1 + rand.nextInt(6)生成1到6之間的隨機數。 – Ben