运算符#

介绍#

VEXcode IQ(第二代)中的操作模块可以帮助机器人处理数字和文字。这些模块可以解决数学问题、比较数值、判断条件真假以及处理字符串。字符串可以用来打印数值和文字。

以下是可用块的列表:

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

  • 比较运算符 – 使用大于、小于或等于运算符比较两个值。

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

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

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

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

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

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

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

  • 余数 – 报告除法运算的余数。

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

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

  • length – 报告字符串中的字符数。

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

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

数学运算符#

数学运算符 报告块对运算符两侧的值执行基本数学运算。它根据项目的打印精度返回结果,默认为 0 位小数,但可以使用 设置屏幕打印精度设置控制台打印精度 块进行调整。

数学运算符报告器块。#
([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 ▶

比较运算符#

比较运算符布尔块报告两个值之间的比较结果是还是

  • 正确 – 比较结果正确(例如,5 > 3 返回 正确)。

  • 错误 – 比较不正确(例如,2 ≥ 5 返回 错误)。

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

范围

描述

值 1

要比较的第一个值。

操作员

要使用的比较:

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

值 2

要比较的第二个值。

例子

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

逻辑运算符#

逻辑运算符布尔块报告多个值之间的逻辑条件是还是

  • 正确 – 逻辑条件已满足。

  • 错误 – 逻辑条件不满足。

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

范围

描述

条件 1

要评估的第一个布尔条件。

操作员

要使用的逻辑运算符:and 如果两个条件都为True,则结果为Trueor 如果至少一个条件为True,则结果为True

条件 2

要评估的第二个布尔条件。

例子

when started
[Display if the ◀ and ▶ buttons are pressed.]
forever
clear screen
set cursor to row [1] column [1] on screen
if <<Brain [ %2 %3> then
print [Buttons pressed!] on screen ▶
结束
结束

非运算符#

非运算符布尔块会反转另一个布尔块的结果。如果输入为,则返回;如果输入为,则返回

  • 正确 – 输入条件通常会报告 错误

  • 错误 – 输入条件通常会报告 正确

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

范围

描述

状况

要反转的布尔条件。

例子

when started
[Turn until the robot is facing left. ]
forever
if <not <(drive heading in degrees) [math_greater_than v] [270]>> then
turn [right v]
else
stop driving

范围运算符#

范围运算符布尔块报告一个值是否在指定的范围内。

  • – 如果该值在范围内。

  • 错误 – 如果该值超出范围。

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

范围

描述

第一操作员

要使用的比较运算符:

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

价值

要检查的值。

第二操作员

要使用的比较运算符:

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

例子

when started
[Turn and display when facing forward or back. ]
turn [right v] for [360] degrees ◀ and don't wait
forever
clear screen
set cursor to row [1] column [1] on screen
if <[270] [math_greater_than v] (drive heading in degrees) [math_greater_than v] [90]> then
print [Facing backward!] on screen ▶
else
print [Facing forward!] on screen ▶

随机选择#

“随机选择”报告模块会报告一个介于最小值和最大值之间的随机正数或负数。它可以同时返回最小值和最大值。例如,选择 1 到 10 可以返回 1 到 10 之间的任意整数。

随机挑选记者区块。#
pick random [1] to [10]

参数

描述

分钟

可以选择的最低可能数字。

最大限度

可以选取的最大数字。

示例

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

整数#

“四舍五入”报告模块会根据标准舍入规则,将给定的数字四舍五入到最接近的整数:

  • 如果小数为 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 ▶

数学函数#

数学函数报告模块将选定的数学函数应用于给定的数字并返回结果。它支持绝对值、四舍五入、平方根、三角函数、对数和指数运算等操作。

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

范围

描述

功能

对输入值应用的数学运算:

  • abs - 绝对值
  • floor - 向下取整
  • ceiling - 向上取整
  • sqrt - 平方根
  • sin - 正弦
  • cos - 余弦
  • tan - 正切
  • asin - 反正弦
  • acos - 反余弦
  • atan - 反正切
  • ln - 自然对数
  • log - 以 10 为底的对数
  • e^ - 欧拉数的幂
  • 10^ - 10 的幂
  • negative - 负值
  • 的负数

数字

应用该函数的数字。

例子

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

反义词#

atan2 报告块以度为单位报告 Y/X 的反正切值,表示正 X 轴与点 (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 ▶

#

余数报告块报告两个值相除运算的余数。

剩余部分报告器块。#
(remainder of [0] / [0])

范围

描述

股利

待除数。

除数

要除以的数字。

例子

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

加入#

join 报告块报告一个由两个输入字符串组合而成的字符串。

加入记者区块。#
(join [apple] [banana])

范围

描述

字符串 1

第一个要合并的字符串。

字符串 2

要合并的第二个字符串。

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

#

字母报告块报告给定字符串中指定位置的单个字符字符串。

快报记者阻拦。#
(letter [1] of [apple])

范围

描述

位置

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

细绳

要从中提取字母的字符串。

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

长度#

长度报告块报告指定字符串中的字符数,包括空格。

长度报告块。#
(length of [apple])

范围

描述

细绳

要测量长度的字符串。

例子

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

包含#

contains 布尔块报告指定的字符串是否包含给定的单词或字符。

  • 正确 – 该字符串包含该特定单词或字符。

  • 错误 – 该字符串不包含该特定单词或字符。

包含布尔块。#
<[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 ▶
结束

转变#

convert 报告块会将数字转换为指定类型(文本或数字)后的值进行报告。

  • text – 将数字转换为字符串。数字必须为字符串格式才能与 字符串运算符 代码块一起使用。

  • 数字 – 将数字转换为十进制值。

转换报告器块。#
(convert [0] to [text v])

范围

描述

价值

要转换的数字。

类型

将数字转换为的类型:

  • 文本
  • 数字

例子

when started
[Display the robot's heading as it turns.]
turn [right v]
forever
clear screen
set cursor to row [1] column [1] on screen
print (join [Currently facing: ] (convert ([BrainInertial v] heading in degrees) to [text v])) on screen ▶
wait [0.1] seconds
结束