LUA(初学)

条件语句if

lua 复制代码
if then
end
lua 复制代码
local a = 2
if a < 6 then
    print(a)
end
lua 复制代码
2

条件语句if else

lua 复制代码
if then
else
end
lua 复制代码
local a = 2
local b = 3
if a > 6 then
    print(a)
else
    print(b)
end
lua 复制代码
3

while循环语句

lua 复制代码
while do
end
lua 复制代码
local a = 2
while  a < 5 do
    a = a + 1
    print(a)
end
lua 复制代码
3
4
5

for循环语句

lua 复制代码
for do
end
lua 复制代码
for a = 1,10,3 do
    print(a)
end
lua 复制代码
1
4
7
10

repeat循环语句

lua 复制代码
repeat 
until
lua 复制代码
local a = 2
repeat 
   a = a + 1
   print(a)
until a > 5
lua 复制代码
3
4
5
6

文本格式化

lua 复制代码
string.format
lua 复制代码
local a = 2
local b = 3
--local c = a.."*"..b.."="..(a * b)
local c = string.format("%d*%d=%d", a, b, a*b)
print(c)
lua 复制代码
2*3=6

循环控制 break/goto

1、跳出整个循环

lua 复制代码
break--结束
lua 复制代码
for a = 1,5 do
    if a == 3 then
        break
    end
    print(a)
end
lua 复制代码
1
2

2、跳出本次循环

lua 复制代码
goto--跳转到后面代号处
lua 复制代码
for a = 1,5 do
    if a == 3 then
        goto abc
    end
    print(a)
    ::abc::
end
lua 复制代码
1
2
4
5

嵌套循环

lua 复制代码
for a = 1,9 do
    for b = 1,a do
        io.write(string.format("%d*%d=%d ", a, b, a*b))
    end
    io.write("\n")
end
lua 复制代码
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

函数定义、调用

lua 复制代码
function
end
lua 复制代码
function say()
    print("say内容1")
    print("say内容2")
end
--不会输出,需要调用(让函数执行)
say()--调用
say()
lua 复制代码
say内容1
say内容2
say内容1
say内容2

函数参数

lua 复制代码
function
end
lua 复制代码
--参数(输入):函数执行之前的已知条件,给函数传递的数据
--返回值(输出):函数执行之后带回来的结果
function say(name)--()内叫参数列表
    print("你好,"..name.."博士")
end
say("大")
lua 复制代码
你好,大博士

return函数返回值

lua 复制代码
return--中止当前函数
lua 复制代码
function say(name)
    return("你好,"..name.."博士")
    --return后不能加东西,会报错
end
local a = say("小")
print (a)
lua 复制代码
你好,小博士

函数参数、返回值

lua 复制代码
function say(a,b)--多个参数以逗号分隔
    return a*b
end
local c = say(2,3)
local d = say(10,3)
print(c,d)
lua 复制代码
6	30
--[[使用print(a, b)这样的方式输出多个参数时,参数之间会自动用制表符(tab)分隔,而不是简单的空格。
这是Lua的print函数默认行为,它会自动在每个参数之间插入一个制表符(ASCII码为9)作为分隔符]]
相关推荐
honder试试21 小时前
焊接自动化测试平台图像处理分析-模型训练推理
开发语言·python
^Rocky21 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
ponnylv21 小时前
深入剖析Spring Boot启动流程
java·开发语言·spring boot·spring
萧邀人1 天前
第一课、Cocos Creator 3.8 安装与配置
开发语言
Jayden_Ruan1 天前
C++逆向输出一个字符串(三)
开发语言·c++·算法
不吃鱼的羊1 天前
启动文件Startup_vle.c
c语言·开发语言
VBA63371 天前
VBA之Word应用第四章第二节:段落集合Paragraphs对象(二)
开发语言
点云SLAM1 天前
C++ 常见面试题汇总
java·开发语言·c++·算法·面试·内存管理
xiaowu0801 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
edjxj1 天前
Qt图片资源导入
开发语言·qt