Lua学习笔记(一)2023.08.08

学习视频:

https://www.bilibili.com/video/BV1vf4y1L7Rb/?spm_id_from=333.337.search-card.all.click&vd_source=5c3f770c8a6964a2c7d2456189ea41a1icon-default.png?t=N6B9https://www.bilibili.com/video/BV1vf4y1L7Rb/?spm_id_from=333.337.search-card.all.click&vd_source=5c3f770c8a6964a2c7d2456189ea41a1

运行环境

LuatOS在线调试工具

LuatOS文档icon-default.png?t=N6B9https://wiki.luatos.com/在线体验LuatOS->Lua测试

(一)Hello World

print("hello world!")

(二)怎么声明变量

举例:声明一个number变量a

a=1
print(a)
输出结果

在Lua中只要赋值了一个变量就表示声明了一个变量,Lua中声明的变量默认是全局的 。

 (三)nil类型

nil类型表示没有任何有效值,只要是没有声明的值就是nil。类似于其他语言的null,不同的是Lua中未声明的变量输出时值为nil,而其他很多语言中会产生报错。

a = 1
print(a)
print(b)
输出结果

(四) 多重赋值

举例1:

a,b=1,2
print(a,b)
输出结果

 举例2:

a,b,c=1,2
print(a,b,c)
输出结果

 (五)Number类型/数值型

a=0x11  --十六进制
b=2e10  --科学计数法
c=10
print(a,b,c)
输出结果

(六)运算符

参考菜鸟编程中的内容

菜鸟编程-Lua运算符部分icon-default.png?t=N6B9https://www.runoob.com/lua/lua-miscellaneous-operator.html补充:在Lua5.3中增加了对左移右移符号的支持

print(1<<3)
print(8>>3)
输出结果

 (七)string类型/字符串

--双引号间的一串字符
str1 = "Lua"
--单引号间的一串字符
str2 = 'Lua'
--[[和]]--间的一串字符
str3 = [[Lua]]
str4 = [[使用双括号时,甚至能包含换行数据
换行了
最后一行]]

--输出所有字符串
print(str1)
print(str2)
print(str3)
print(str4)
输出结果

转义字符

r = 'ab\\cd\"ef\'g\\h]]'
print(str)
输出结果

参考

菜鸟教程字符串部分icon-default.png?t=N6B9https://www.runoob.com/lua/lua-strings.html

 字符串拼接

print('abc'..'def')
输出结果

 (八)function函数

function function_name(参数1,参数2,...)
    --方法体
end
function_name = function(参数1,参数2,...)
    --方法体
end

猜你喜欢

转载自blog.csdn.net/weixin_46041788/article/details/132165915