EV3 Scratch 指南

EV3 Scratch 指南 > Statistics(统计)

Statistics 统计

In this section we look at a list of numbers and calculate
在本节中,我们查看一个数字列表并计算

  • minimum value and its position
    最小值及其位置
  • maximum value and its position
    最大值及其位置
  • sum  和
  • average  平均

Random list  随机列表

For this exercice we use a list with 8 random numbers between 1 and 99. This way we can print them on the first line of the display.
在这个练习中,我们使用一个包含 1 到 99 之间的 8 个随机数的列表。这样我们就可以将它们打印在显示屏的第一行。

Statistics  统计

We call this function in the start event and also with the left button.
我们在 start 事件中调用此函数,也使用 left 按钮调用此函数。

Statistics  统计

The result of this function looks like this:
此函数的结果如下所示:

86 35 49 54 37 6 93 62 

Calculate the minimum  计算最小值

At the first iteration i=1 we set
在第一次迭代 i=1 时,我们设置

  • min = list[1]  min = 列表[1]
  • minpos = 1

Then we iterate through the rest of the list. If we find a number which is smaller, we take it as the new minimum.
然后我们遍历列表的其余部分。如果我们找到一个较小的数字,我们将其作为新的最小值。

  • min = list[i]
  • minpos = i
Statistics  统计

We call this function with the down button.
我们用向下按钮调用这个函数。

Statistics  统计

The result of this function looks like this:
此函数的结果如下所示:

86 35 49 54 37 6 93 62 
min = 6 
at 6 

Calculate the maximum  计算最大值

Again, at the first iteration i=1 we set
同样,在第一次迭代 i=1 时,我们设置

  • max = list[1]  最大 = 列表 [1]
  • maxpos = 1  最大位置 = 1

Then we iterate through the rest of the list. If we find a number which is larger, we take it as the new maximum.
然后我们遍历列表的其余部分。如果我们找到一个更大的数字,我们将其作为新的最大值。

  • max = list[i]  最大值 = list[i]
  • maxpos = i  最大位置 = i
Statistics  统计

We call this function with the up button.
我们用向上按钮调用这个函数。

Statistics  统计

The result of this function looks like this:
此函数的结果如下所示:

86 35 49 54 37 6 93 62 
max = 93 
at 7 

Calculate sum and average
计算总和和平均值

To get the sum we add all elements of the list together. The average is obtained by dividing the sum by the number of elements.
为了得到总和,我们将列表的所有元素相加。通过将总和除以元素数来获得平均值。

Statistics  统计

We call this function with the center button.
我们用中心按钮调用这个函数。

Statistics  统计

The result of this function looks like this:
此函数的结果如下所示:

86 35 49 54 37 6 93 62 
max = 422 
avg = 52.75