变量#

简介#

The Variable blocks in VEXcode AIM let you store information that your project can use later. A variable is like a labeled container that holds a value, such as a number, a Boolean, or a list of items.

You would use a variable when you want your project to remember something while it is running. For example, a variable could keep track of how many times the robot has moved, how many objects it has counted, or whether a goal has been reached.

Variables are helpful when the information in a project might change. Instead of writing the same number or answer in many places, you can store it in a variable and update it as the project runs. This makes your project easier to change, check, and understand.

In VEXcode AIM, Variable blocks can store different kinds of information, including numbers, Boolean values such as true or false, lists, and 2D lists. These blocks are useful when a project needs to count, make decisions, track progress, or organize information.

Below is a list of available blocks:

数值变量——存储和修改数字。

布尔变量——存储真或假值。

列表——存储和管理值的集合。

2D列表——以网格形式存储和管理值集合。

数值变量#

数字变量#

The numeric variable reporter block reports the numeric value assigned to the variable.

数字变量报告指令块。#
(my_variable)

参数

描述

该指令块没有参数。

示例

when started
[Display the value of the variable.]
set [deliveries v] to [2]
print (deliveries) on screen ▶

设置数字变量#

The set numeric variable stack block assigns a value to a numeric variable.

参数

描述

变量

要赋值的数字变量。用户可以选择现有变量、重命名或删除它。

分配给变量的数字(可以是整数、小数或数字块)。

示例

when started
[Display the value of the variable.]
set [deliveries v] to [2]
print (deliveries) on screen ▶

改变数值变量#

The change numeric variable stack block increases or decreases a variable’s value by a given amount.

改变数值变量指令段。#
change [my_variable v] by (1)

参数

描述

变量

要修改的数字变量。用户可以选择现有变量、重命名或删除它。

增加或减少变量的量。

示例

when started
[Deliver a package twice.]
set [deliveries v] to [0]
move [forward v] for (100) [mm v] ▶
change [deliveries v] by [1]
move [forward v] for (100) [mm v] ▶
change [deliveries v] by [1]
print (deliveries) on screen ▶

布尔变量#

布尔变量#

The Boolean variable Boolean block reports either a True or False value.

布尔变量报告块。#
<my_boolean>

参数

描述

该指令块没有参数。

示例

when started
[Display a message if the variable is set to True.]
set [my_boolean v] to <true v>
if <my_boolean> then
print [Boolean is True!] on screen ▶
结束

设置布尔变量#

The set Boolean variable stack block assigns a True or False value to a Boolean variable.

设置布尔变量指令段。#
set [my_boolean v] to <true v>

参数

描述

变量

要赋值的布尔变量。用户可以选择现有变量、重命名或删除它。

布尔值(真或假)。

示例

when started
[Repeat until 3 deliveries are made.]
set [finished v] to <false v>
set [deliveries v] to [0]
repeat until <finished>
move [forward v] for (100) [mm v] ▶
change [deliveries v] by [1]

if <(deliveries) [math_equal v] [3]> then
set [finished v] to <true v>

列表#

列表项#

The list item reporter block reports the value of an item in a list. A list is a variable that can store multiple variables of the same type.

列表项报告指令块。#
(item (1) of [my_list v])

参数

描述

索引

列表中项目的位置(从 1 开始)。

变量

The list variable to report an item from. Users can select an existing list, rename it, or delete it.

示例

when started
[Display the delivery locations.]
设定 [delivery_spots v] 为 [Red House] [Green House] [Blue House]
move [forward v] for (100) [mm v] ▶
print (item (1) of [delivery_spots v]) on screen ▶
move [forward v] for (100) [mm v] ▶
print (item (2) of [delivery_spots v]) on screen ▶
move [forward v] for (100) [mm v] ▶
print (item (3) of [delivery_spots v]) on screen ▶

替换列表项#

The replace list item stack block updates a specific item in a list.

替换列表项指令段。#
replace item (1) of [my_list v] to (1)

参数

描述

变量

包含要修改的项目的列表变量。用户可以选择现有列表、重命名或删除它。

索引

列表中项目的位置(从 1 开始)。

分配给指定列表项的新值。

示例

when started
[Replace the first item in a list with "Sports Ball".]
设定 [my_list v] 为 [Robot] [Orange Barrel] [Blue Barrel]
replace item (1) of [my_list v] to [Sports Ball]
print (item (1) of [my_list v]) on screen ▶

设置列表项#

The set list items stack block sets every item in the list to the entered values.

设置列表项指令段。#
设定 [my_list v] 为 [0] [0] [0]

参数

描述

变量

需要赋值的列表变量。用户可选择现有列表、重命名或删除它。

需要存储到列表中的值,需以独立项的形式逐个输入。

示例

when started
[Display the last item in a list.]
设定 [my_list v] 为 [Red] [Green] [Blue]
print (item (3) of [my_list v]) on screen ▶

列表长度#

The length of list reporter block reports the number of items in a list.

列表长度报告指令块。#
(length of [my_list v] :: list :: list)

参数

描述

变量

The list variable to report the length of. Users can select an existing list, rename it, or delete it.

示例

when started
[Display how many items are in a list.]
设定 [my_list v] 为 [Red] [Green] [Blue]
print (length of [my_list v] :: list) on screen ▶

二维列表#

二维列表项#

The 2D list item reporter block reports the value of an item in a 2D list. 2D lists are variables that store multiple list variables.

2D 列表项报告指令块。#
(item (1) (1) of [my_2d_list v])

参数

描述

元素所在的行(从 1 开始计数)。

元素所在的列(从 1 开始计数)。

变量

The 2D list variable to report an item from. Users can select an existing 2D list, rename it, or delete it.

示例

when started
[Display an item from a 2D list.]
设定 [my_2d_list v] 为 ([1][Orange Barrel]) ([2][Blue Barrel])
print (item (2) (2) of [my_2d_list v]) on screen ▶

替换二维列表项#

The replace 2D list item stack block updates a specific item in a 2D list.

替换二维列表项指令段。#
replace item (1) (1) of [my_2d_list v] to (1)

参数

描述

元素所在的行索引(从 1 开始计数)。

元素所在的列索引(从 1 开始计数)。

变量

包含待修改元素的二维列表变量。用户可通过下拉菜单选择现有二维列表、重命名或删除它。

赋给指定二维列表项的新值。

示例

when started
[Replace a value in a 2D list and print the updated item.]
设定 [my_2d_list v] 为 ([1][Orange Barrel]) ([3][Blue Barrel])
replace item [2] [2] of [my_2d_list v] to [Sports Ball]
print (item (2) (2) of [my_2d_list v]) on screen ▶

设置二维列表项#

The set 2D list items stack block sets every item in the 2D list to the entered values.

设置二维列表项指令块。#
设定 [my_2d_list v] 为 ([0][0]) ([0][0])

参数

描述

变量

需要赋值的二维列表变量。用户可选择现有二维列表、重命名或删除它。

需要存储到二维列表中的值,需逐行输入。

示例

when started
[Display an item from a 2D list.]
设定 [my_2d_list v] 为 ([1][Orange Barrel]) ([2][Blue Barrel])
print (item (1) (2) of [my_2d_list v]) on screen ▶

二维列表的长度#

The length of 2D list reporter block reports the number of rows or columns in a 2D list.

2D 列表长度报告指令块。#
length of [my_2d_list v] [rows v]

参数

描述

变量

需要检查长度的二维列表变量。用户可选择现有二维列表、重命名或删除它。

行/列

Whether to report the number of rows or columns.

示例

when started
[Set a 2D list and print the number of rows it has.]
设定 [my_2d_list v] 为 ([1][Orange Barrel]) ([3][Blue Barrel])
print (length of [my_2d_list v] [rows v]) on screen ▶