1、开发环境
去网上下载一个 Lua for Windows
下载地址:
2、lua扩展名 .lua
3、快速入门
1、helloworld
print "hello world"print("hello world") --注释
多行注释
--[[for i= 1, 7,1 do print(revdays[i])end--]]
2、数据类型: nil、 Booleans、 Numbers 、Strings、functions
3、表达式:
算数运算 : + – * / –(一元运算)
关系运算 : < > <= >= == ~=
逻辑运算 : and or not
连接运算符: .. --两个点
print("hello".."jack")
优先级
表的构造
days = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat"}print(days[5])
4、基本语法:
赋值语句
a = "hello".."world" --赋值语句a,b,c,d= 1,2,'c',{ 1}
局部变量和代码块
--局部变量和代码块 localx = 10local i = 1 while i <= x do local x = i * 2 print(x) i = i + 1end
循环和控制结构
print("enter a number")n = io.read("*number")if n < 10 then print("我小于10")elseif n < 100 then --elseif 不是else if print("小于100")else print("其他")end --最后要加end
days = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat"}for key,value in pairs(days) do --构造pairs print(value)endrevdays = {}for i, v in ipairs(days) do revdays[v] = iendfor key, value in pairs(revdays) do print(key.." "..value)end
5、函数
function maxium(a) local mi = 1 --maxium index local m = a[mi] --maxium value for i,val in ipairs(a) do if val > m then mi = i m = val end end return m, miendprint(maxium({ 3,64,9,10,32}))