运算符#

简介#

The Operator blocks in VEXcode AIM help the robot work with numbers and words. These blocks can solve math problems, compare values, evaluating if conditions are true or false, and working with strings. Strings can be used to print values and words.

以下列表是可用的指令块:

  • 数学运算符 – 执行加法、减法、乘法或除法。

  • comparison operator – Compares two values using greater than, less than, or equal to operators.

  • 逻辑运算符 – 使用 and 或 or 评估多个条件。

  • 非运算符 – 反转布尔值。

  • 范围运算符 – 检查数字是否在某个范围内。

  • 随机选择 – 生成指定范围内的随机数。

  • 取整 – 将数字四舍五入为最接近的整数。

  • 数学函数 – 应用平方根、三角、对数和指数等运算。

  • atan2 – 计算 y/x 的反正切。

  • remainder – Reports the remainder of a division operation.

  • 连接 – 将两个字符串合并为一个。

  • 提取字符 – 按位置从字符串中提取字符。

  • length – Reports the number of characters in a string.

  • 是否包含 – 检查字符串是否包含特定的单词或字符。

  • 类型转换 – 将数字转换为文本、整数或小数。

数学运算符#

The math operator reporter block performs basic math operations on the values placed on either side of the operator. It returns a result based on the project’s print precision, which defaults to 0 decimal places but can be adjusted using the set print precision on screen or set print precision on console blocks.

数学运算符报告指令块。#
([0] [math_plus v] [0])

参数

描述

值 1

运算中使用的第一个数字。

运算符

可使用的数学运算符:

  • + - 加法
  • - - 减法
  • * - 乘法
  • / - 除法

值 2

运算中使用的第二个数字。

示例

when started
[Perform basic addition and show two decimal places.]
set print precision to [0.01 v] on screen
print ([10] [math_plus v] [10.5]) on screen ▶

比较运算符#

The comparison operator Boolean block reports whether the comparison between two values is True or False.

  • True – The comparison is correct (e.g., 5 > 3 returns True).

  • False – The comparison is incorrect (e.g., 2 ≥ 5 returns False).

比较运算符布尔指令块。#
<[0] [math_equal v] [0]>

参数

描述

值 1

要比较的第一个值。

运算符

可使用的比较:

  • = - 等于
  • < - 小于
  • - 小于或等于
  • > - 大于
  • - 大于或等于

值 2

要比较的第二个值。

示例

when started
[Turn right until the heading reaches 90 degrees.]
turn [right v]
wait [0.05] seconds
wait until <(heading in degrees) [math_greater_than v] [90]>
stop all movement

逻辑运算符#

The logical operator Boolean block reports whether the logical condition between multiple values is True or False.

  • True – The logic condition is met.

  • False – The logic condition is not met.

逻辑运算符布尔指令块。#
<<> [and v] <>>

参数

描述

条件 1

The first Boolean condition to check.

运算符

The logical operator to use: and reports True if both conditions are True. or reports True if at least one condition is True.

条件 2

The second Boolean condition to check.

示例

when started
[Kick a held sports ball when the screen is pressed.]
forever
if <<has [sports ball v]?> [and v] <screen pressed?>> then
kick object [hard v]

非运算符#

The not operator Boolean block inverts the result of a Boolean block. If the input is True, it reports False, and if the input is False, it reports True.

  • True – The input condition would normally report False.

  • False – The input condition would normally report True.

非运算符布尔指令块。#
<not <>>

参数

描述

条件

要反转的布尔条件。

示例

when started
[Turn until a sports ball or barrel is detected.]
forever
get [all cargo v] data from AI Vision
if <not <AI Vision object exists?>> then
turn [right v]
else
stop all movement

范围运算符#

The range operator Boolean block reports whether a value falls within a specified range.

  • True – If the value is within the range.

  • False – If the value is outside the range.

范围运算符布尔块。#
<[0] [math_less_than v] [0] [math_less_than v] [0]>

参数

描述

第一个操作符

可使用的比较运算符:

  • < - 小于
  • ≤ - 小于或等于
  • - 大于
  • ≥ - 大于或等于

要检查的值。

第二个操作符

可使用的比较运算符:

  • < - 小于
  • ≤ - 小于或等于
  • - 大于
  • ≥ - 大于或等于

示例

when started
[Move forward and report when out of bounds.]
move [forward v] for [100] [mm v] ◀ and don't wait
forever
clear screen
set cursor to row [1] column [1] on screen
if <[25] [math_less_than v] ([y v] position in [mm v]) [math_less_than v] [75]> then
print [In Bounds.] on screen ▶
else
print [Out of Bounds!] on screen ▶

随机数选择#

The pick random reporter block reports a random positive or negative number between a minimum and a maximum. It can return both the min value and the max value. For example, picking 1 to 10 can return any whole number from 1 through 10.

随机数选择报告指令块。#
pick random [1] to [10]

参数

描述

最小

可被选取的最小数值。

最大

可被选取的最大数值。

示例

when started
[Display a number between 1 and 10.]
print (pick random [1] to [10]) on screen ▶

when started
[Display a decimal number between 1 and 10.5.]
set print precision to [All Digits v] on screen
print (pick random [1] to [10.5]) on screen ▶

取整数值#

The round number reporter block reports the given number rounded to the nearest whole number, following standard rounding rules:

  • 若小数部分大于或等于 0.5,则向上舍入。

  • 若小数部分小于 0.5,则向下舍入。

取整数值报告指令块。#
(round [0] to [0] decimal places)

参数

描述

数字

需要舍入的原始数值。

小数位数

指定舍入后保留的小数位数。

示例

when started
[Display the rounded result of 10 / 3.]
set print precision to [0.01 v] on screen
print (round ([10] [math_division v] [3]) to [1] decimal places) on screen ▶

数学函数#

The math functions reporter block applies a selected mathematical function to a given number and returns the result. It supports operations such as absolute value, rounding, square roots, trigonometric functions, logarithms, and exponentiation.

数学函数报告指令块。#
[abs v] of [0]

参数

描述

函数

The mathematical operation to apply to the input value:

  • abs - Absolute value
  • floor - Rounds down
  • ceiling - Rounds up
  • sqrt - Square root
  • sin - Sine
  • cos - Cosine
  • tan - Tangent
  • asin - Inverse sine
  • acos - Inverse cosine
  • atan - Inverse tangent
  • ln - Natural logarithm
  • log - Base 10 logarithm
  • e^ - Euler’s number raised to a power
  • 10^ - 10 raised to a power
  • negative - Reports the negative of the number

数字

需应用该函数的数值。

示例

when started
[Display the square root of 16.]
print ([sqrt v] of [16]) on screen ▶

atan2#

The atan2 reporter block reports the inverse tangent of Y/X as an angle in degrees, representing the angle between the positive X-axis and the point (X, Y).

(atan2 of x:[4] y:[3])

参数

描述

x

点的水平坐标位置,以原点 (0,0) 为基准测量。

y

点的垂直坐标位置,以原点 (0,0) 为基准测量。

示例

when started
[Calculate the angle from current position (4, 3).]
print (atan2 of x:[4] y:[3]) on screen ▶

取余#

The remainder reporter block reports the remainder of the division operation between two values.

取余报告指令块。#
(remainder of [0] / [0])

参数

描述

被除数

需要被除的数值。

除数

要除以的数字。

示例

when started
[Display the remainder of 10 / 3.]
print (remainder of [10] / [3]) on screen ▶

连接#

The join reporter block reports a single string that combines two input strings.

连接报告指令块。#
(join [apple] [banana])

参数

描述

字符串 1

第一个要合并的字符串。

字符串 2

要合并的第二个字符串。

when started
[Display "VEXcode" on the screen.]
print (join [VEX] [code]) on screen ▶

提取字符#

The letter reporter block reports a single-character string from the specified position in the given string.

提取字符报告指令块。#
(letter [1] of [apple])

参数

描述

位置

字符串中字符的位置(从 1 开始)。

字符串

需要从中提取字符的原始字符串。

when started
[Display the first letter of "Robot".]
print (letter (1) of [Robot]) on screen ▶

长度#

The length reporter block reports the number of characters in the specified string, including spaces.

字符串长度报告指令块。#
(length of [apple])

参数

描述

字符串

要测量长度的字符串。

示例

when started
[Count the number of characters in "VEX Robotics".]
print (length of [VEX Robotics]) on screen ▶

是否包含#

The contains Boolean block reports whether the specified string includes the given word or character.

  • True – The string includes that specific word or character.

  • False – The string does not include that specific word or character.

是否包含布尔指令块。#
<[apple] contains [a]?>

参数

描述

字符串

被搜索的主字符串。

搜索项

需要在字符串中查找的词或字符。

示例

when started
[Check if "robotics" contains "bot".]
if <[robotics] contains [bot]?> then
print [The word contains "bot".] on screen ▶
else
print [Not found.] on screen ▶
结束

类型转换#

The convert reporter block reports the value of a number converted to the specified type, either as text or a number.

  • text – Converts the number to a string. Numbers must be in string format to work with String Operator blocks.

  • number – Converts the number to a decimal value.

类型转换报告指令块。#
(convert [0] to [text v])

参数

描述

需要转换的原始数字。

类型

The type to convert the number into: text converts the value to a string, and number converts the value to a decimal.

示例

when started
[Add any number to 5.]
ask [Give me a number.] and wait
print ((convert (answer) to [number v]) [math_plus v] [5]) on screen ▶