> 

 > 

4. 以积木式编程制作网络应用程式
数学
数学

本文档介绍了基本的数学运算,包括加法、减法、乘法、除法、设置和转换数字、查找余数、最小值和最大值、绝对值、平方根、近似值以及生成随机值。

将两个值相加/相乘

0 + 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_arithmetic\"><field name=\"OP\">ADD</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

这用于返回两个数字的总和。

参数: 条件:

  • 数字(预设值:0)
  • 运算符:+(默认)/-/×/÷/**

输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>count</variable><variable>add</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"variables_set\"><field name=\"VAR\">add</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">ADD</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">2</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">add</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

添加 - 输出

将两个数字除以

0 ÷ 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_arithmetic\"><field name=\"OP\">DIVIDE</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于返回两个数字之间的商。

参数: 条件:

  • 数字(预设值:0)
  • 运算符:+(默认)/-/×/÷/**

输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>count</variable><variable>divide</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"variables_set\"><field name=\"VAR\">divide</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">DIVIDE</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">2</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">divide</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}

当计数为 ÷ 2 时,输出:

除法 - 输出

当计数为**2时,输出:

功率 - 输出

设置数字

0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_number\"><field name=\"NUM\">0</field></block></xml>"}

此块用于将值设置为数字或十进制数。

参数: 条件:数字(默认值:0)
输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>count</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">6.1</field></shadow></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

数字 - 输出

显示两个数字之间的余数

提醒 0 ÷ 1

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_modulo\"><value name=\"DIVIDEND\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"DIVISOR\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></xml>"}

此块用于显示两个值之间的余数。

参数: 条件:数字(默认值:0)
输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>count</variable><variable>remainder</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"variables_set\"><field name=\"VAR\">remainder</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_modulo\"><value name=\"DIVIDEND\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"DIVISOR\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">remainder</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

余数 - 输出

查找最小值

最小值为 0 和 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_op2\"><field name=\"op\">min</field><value name=\"x\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"y\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

“最小值 0 和 0”块用于提取两个数字之间的较小一个。

参数: 条件:数字(默认值:0)
输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>count</variable><variable>minimum</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value><next><block type=\"variables_set\"><field name=\"VAR\">minimum</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_op2\"><field name=\"op\">min</field><value name=\"x\"><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"y\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">minimum</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

最小值 和 - 输出

查找最大值

最大值 0 和 0

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_op2\"><field name=\"op\">max</field><value name=\"x\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"y\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于在两个数字之间取较大的值。

参数: 条件:数字(默认值:0)
输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>count</variable><variable>maximum</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">count</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">3.568</field></shadow></value><next><block type=\"variables_set\"><field name=\"VAR\">maximum</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_op2\"><field name=\"op\">max</field><value name=\"x\"><block type=\"variables_get\"><field name=\"VAR\">count</field></block></value><value name=\"y\"><shadow type=\"math_number\"><field name=\"NUM\">4</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">maximum</field></block></value></block></value></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

最大值和 - 输出

将数字更改为绝对值

0 的绝对值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_op3\"><value name=\"x\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于将数字的值转换为绝对值。

参数: 条件:数字(默认值:0)
输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>absolute</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">absolute</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_op3\"><value name=\"x\"><shadow type=\"math_number\"><field name=\"NUM\">-10</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">absolute</field></block></value></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

绝对值 - 输出

取平方根

平方根

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_js_op\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" op-type=\"unary\"></mutation><field name=\"OP\">sqrt</field><value name=\"ARG0\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于取值的平方根。

参数: 条件:

  • 数字(预设值:0)
  • 平方根 (预设)/sin/cos/tan/atan2/整数除/整数乘

输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>value</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">value</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_js_op\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" op-type=\"unary\"></mutation><field name=\"OP\">sqrt</field><value name=\"ARG0\"><shadow type=\"math_number\"><field name=\"NUM\">144</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">value</field></block></value></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}

当它设置为平方根 144 时,输出:

平方根 - 输出

当它设置为 sin 144 时,输出:

Sin - 输出

当它设置为 cos 144 时,输出:

Cos - 输出

当它设置为 tan 144 时,输出:

Tan - 输出

当它设置为 atan2 144 0 时,输出:

Atan2 - 输出

当它设置为 144 整数÷ 5 时,输出:

整数除法 - 输出

当它设置为 144 整数× 5 时,输出:

整数乘法 - 输出

将数字转换为近似值

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"math_js_round\"><field name=\"OP\">round</field><value name=\"ARG0\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

此块用于将数字替换为近似值。 如果小数部分超过二分之一,则该数字将增加。

参数: 条件:

  • 数字(预设值:0)
  • 圆形(默认)/天花板/地板/截断

输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>value</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">value</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_js_round\"><field name=\"OP\">round</field><value name=\"ARG0\"><shadow type=\"math_number\"><field name=\"NUM\">5.11</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">value</field></block></value></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}

当它设置为四舍五入 5.11 时,输出:

圆形 - 输出

当它设置为上限 5.11 时,输出:

天花板 - 输出

当它设置为下限 5.99 时,输出:

地板 - 输出

当它设置为截断 5.99 时,输出:

截断 - 输出

选择随机值

随机选择 0 到 10

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"device_random\"><value name=\"min\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"limit\"><shadow type=\"math_number\"><field name=\"NUM\">10</field></shadow></value></block></xml>"}

此块用于在范围之间选择随机数。

参数: 条件:数字(默认值:0 和 10)
输出:
示例代码:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>value</variable><variable>dateTimeOffset</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"variables_set\"><field name=\"VAR\">value</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"device_random\"><value name=\"min\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"limit\"><shadow type=\"math_number\"><field name=\"NUM\">10</field></shadow></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">value</field></block></value></block></value></block></value></block></next></block></next></block></next></block></statement></block></xml>"}

输出:

随机选择 - 输出

目录