1
public class ArrayStudentPoll {
public static void main(String args[])
{
// array of survey responses
int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6,
10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6,
4, 8, 6, 8, 10 };
int frequency[] = new int[ 11 ]; // array of frequency counters
// for each answer, select responses element and use that value
// as frequency index to determine element to increment
for (int answer = 0; answer < responses.length; answer++)
++frequency[ responses[ answer ] ];
System.out.printf("%s%10s\n", "Rating", "Frequency");
// output each array element's value
for (int rating = 1; rating < frequency.length; rating++)
System.out.printf("%6d%10d\n", rating, frequency[ rating ]);
}
}
这是我在我的项目中看到的代码。pre incrementing数组对象(++ frequency [i];)是什么意思?
我所了解的一切,但部分++frequency[i];
频率是一个数组而不是数字?那究竟是在做什么?他们写了评论,但仍然没有得到他们。
这里是我失控它
Rating Frequency
1 2
2 2
3 2
4 2
5 5
6 11
7 5
8 7
9 1
10 3
但是那个循环的逻辑是什么?你能解释一下吗? –
@javadevabhi它计算每个数字出现在'respond'数组中的次数。在循环频率[i]'后面将包含“响应”数组中出现“i”的次数。 – Eran