简体   繁体   English

需要帮助理解代码

[英]Need help understanding code

I am taking a C# class and I need help understanding the following code. 我正在学习C#课程,我需要帮助理解以下代码。

The code has an array which represents responses to a survey, with values 1 thru 10. 代码有一个数组,表示对调查的响应,值为1到10。

The output displays these ratings and the frequency of how many times a value was selected. 输出显示这些评级以及选择值的次数。

The following code is from my book, but I have modified it to just a basic example. 以下代码来自我的书,但我已将其修改为一个基本示例。

int[] responses = { 3, 2, 5, 6, 3, 5 , 4, 5, 5, 5};
int[] frequency = new int[7];


for (int answer = 0; answer < responses.Length; answer++)
   ++frequency[responses[answer]];

for (int rating = 1; rating < frequency.Length; rating++)
   Console.WriteLine(rating + ", " + frequency[rating]);

Console.Read();

How does the line ++frequency[responses[answer]]; 线路++frequency[responses[answer]];如何++frequency[responses[answer]]; work? 工作? In looking at this, if I take reponses[answer] the first time through the loop, this would represent responses[0] which would be a 3, correct? 在看这个时,如果我第一次通过循环接受reponses[answer] ,这将代表responses[0] ,这将是3,正确吗? This is where I get confused, what does the ++frequency part of this line do? 这是我感到困惑的地方,这条线的++frequency部分是做什么的?

frequency[responses[answer]] = frequency[responses[answer]] + 1;

EDIT: I think it's pretty unclear to write it like that. 编辑:我认为这样写是非常不清楚的。 As a personal preference, I don't like using unary operations (++x, x++, etc) on elements that have lots of indexes present. 作为个人偏好,我不喜欢对存在大量索引的元素使用一元操作(++ x,x ++等)。

It adds one to the frequency at that location in the array. 它在阵列中该位置的频率上加一。

For example, the frequency at position 3 (from your example) will be increased by one after that line executes. 例如,在该行执行后,位置3(来自您的示例)的频率将增加1。

EDIT: So, in more detail, when answer = 0, responses[0] = 3, so frequency[3] gets one added to it. 编辑:因此,更详细地说,当answer = 0时,响应[0] = 3,所以频率[3]会添加一个。

The ++ could very easily be at the end of the command as well. ++也可以很容易地在命令的末尾。 In other words, 换一种说法,

++frequency[responses[answer]];


is the same thing (IN THIS CASE) as using 和使用一样(在这种情况下)

frequency[responses[answer]]++;

Let's break it down: As you point out, on the first pass responses[answer] will evaluate to "3" 让我们分解一下:正如你所指出的那样,在第一次通过时,回答[answer]将评估为“3”

So this then looks like ++frequency[3] 所以这看起来像++频率[3]

The ++ is incrementing the value of the array at index 3 by 1 ++将索引3处的数组值递增1

Simple enough? 够简单吗?

I should also point out that applying the ++ before the array rather than after it does effect how the incrementing is executed (although it doesn't effect the results of this code). 我还应该指出,在数组之前而不是之后应用++会影响递增的执行方式(尽管它不会影响此代码的结果)。

For instance: 例如:

int n = 2;
int j = ++n;
int k = n++;

What are j and k? 什么是j和k?

j will be 3, and k will also be 3. This is because if you place the ++ before, it evaluates it first. j将是3,k也将是3.这是因为如果你之前放置++,它首先评估它。 If you place it at the end, it evaluates it after the rest of the expression. 如果将它放在最后,它会在表达式的其余部分之后对其进行求值。

If it helps, think of ++frequency[] as "frequency = frequency + 1". 如果有帮助,可以将++ frequency []视为“frequency = frequency + 1”。

If the ++ operator comes before the variable, then the increment is applied before the statement is executed. 如果++运算符位于变量之前,则在执行语句之前应用增量。 If the ++ comes afterwards, then the statement is executed and then the variable is incremented. 如果后来出现++,则执行该语句,然后递增变量。

In this case, it doesn't matter, since incrementing before or after doesn't impact the logic. 在这种情况下,无关紧要,因为在之前或之后递增不会影响逻辑。

Since "responses[answer]" evaluates to a number, that line of code is incrementing the frequency entry at that array index. 由于“响应[回答]”评估为一个数字,该行代码正在递增该数组索引处的频率条目。 So the first time through, answer is 0, so responses[answer] is 3, so the frequency[3] box is getting incremented by 1. The next time through, the frequency[2] box is incremented... etc. etc. etc. 所以第一次通过,答案是0,所以响应[答案]是3,所以频率[3]框增加1.下一次,频率[2]框增加......等等等等

frequency is an array, where all elements are initialized to 0 (the default value for an int). frequency是一个数组,其中所有元素都初始化为0(int的默认值)。 The line ++frequency[responses[answer]] will increment the frequency element pointed out by the integer found at responses[answer] . ++frequency[responses[answer]]将增加在responses[answer]找到的整数指出的频率元素。 By putting the ++ in front of frequency, the array element will be incremented before the resulting value is returned. 通过将++放在频率前面,数组元素将在返回结果值之前递增。

You can read more about the ++ operator here . 您可以在此处阅读有关++运算符的更多信息

In cases like this it's often useful to rewrite the code as you walk it. 在这种情况下,在您走过时重写代码通常很有用。

When answer = 0 当答案= 0时

  • ++frequency[responses[0]] ++频率[响应[0]]
  • ++frequency[3] since responses[0] = 3 ++频率[3],因为响应[0] = 3
  • frequency now looks like { 0, 0, 0, 1, 0, 0, 0 } 频率现在看起来像{0,0,0,1,0,0,0}

When answer = 1 当答案= 1时

  • ++frequency[responses[1]] ++频率[响应[1]]
  • ++frequency[2] since responses[1] = 2 ++频率[2],因为响应[1] = 2
  • frequency now looks like { 0, 0, 1, 1, 0, 0, 0 } 频率现在看起来像{0,0,1,1,0,0,0}

And so on. 等等。

It means increment the value at frequency[ 3 ]. 它意味着在频率[3]处递增值。 Where 3 is the return result from responses[answer]. 其中3是回复的结果[回答]。 Similarly the next iteration would increment the value at frequency[ 2 ]. 类似地,下一次迭代将在频率[2]处递增值。

The ++ operator in C# when applied to an integer will increment it by one. C#中的++运算符应用于整数时会将其递增1。

The specific line you're looking at, frequency is an array of integers with 7 elements. 您正在查看的特定行,frequency是一个包含7个元素的整数数组。 Which is sort of confusing, because the way you explained it in your code, it would appear that this code would break with any value in the responses array above 6. 这有点令人困惑,因为你在代码中解释它的方式,看起来这个代码会破坏6以上的响应数组中的任何值。

That issue aside, basically it's incrementing whichever index of the array it's accessing. 抛开这个问题,基本上它正在递增它正在访问的数组的哪个索引。 So in your example responses[0] would be 3. So this line would find the value of frequency[3] and increment it by 1. Since integer arrays are initialized with all values at zero, then after the first iteration, frequency[3] would be 1. Then if there was another 3 later in your responses array, frequency[3] would be incremented again (ie responses[4]). 因此在您的示例中,响应[0]将为3.因此,此行将找到频率[3]的值并将其递增1.由于整数数组初始化时所有值均为零,因此在第一次迭代后,频率[3]然后如果你的响应数组中有另外3个,则频率[3]将再次递增(即响应[4])。

I hope this helps you. 我希望这可以帮助你。

The goal of the code snippet seems to be to determine the number of times each response appears in the 'responses' array. 代码段的目标似乎是确定每个响应出现在“响应”数组中的次数。 So, for your example set, frequency[3] should be 5, frequency[5] should be 5, etc. 因此,对于您的示例集,频率[3]应为5,频率[5]应为5,等等。

So, the line you are asking about takes the current element from the responses array, and increments the associated value in the frequency array by 1, to indicate that the particular value has been observed in responses. 因此,您询问的行从响应数组中获取当前元素,并将频率数组中的关联值增加1,以指示已在响应中观察到特定值。

Once the entire code snippet executes, the frequency array contains the number of times each element from 0 to 7 was observed in the responses array. 整个代码片段执行后,频率数组包含在响应数组中观察到0到7的每个元素的次数。

It is using the frequency array to count how many times each response was entered. 它使用频率数组来计算每个响应输入的次数。 You could have a counter for each answer: 可以为每个答案都有一个计数器:

int numberOfOnes = 0;
int numberOfTwos = 0;
// Etc...

But that would be ugly programming and not as easy or efficient. 但这将是丑陋的编程,而不是那么容易或有效。 Using the frequency array allows you do not use an if/else if block or a switch and makes your code easier to read. 使用频率数组允许您不使用if / else if块或开关,使代码更易于阅读。

Another thing about that frequency array. 关于频率数组的另一件事。

int[] frequency = new int[7]; 

This initializes all the integers in the array to 0, that's why you can just start off by incrementing it instead of seeing if it was the first time for that specific response and then initializing it with 1 or something of that nature. 这会将数组中的所有整数初始化为0,这就是为什么你可以通过递增它来开始,而不是看它是否是第一次进行特定响应,然后用1或类似的东西初始化它。

Good luck with all the fun C# you have ahead of you. 祝你们拥有所有有趣的C#,祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM