a =7 --全局变量
function jock() --函数
d = 8 -- 默认全局变量
local c=9 --local表示局部变量
end
jock() --执行方法
print(a,d,c)
a = "hello" .."world" --字符串拼接
a=a .. "ddd"
print(a)
while(d>0) --while循环
do
print(d)
d=d-1
end
for ij=0,10,1 do --for循环 0-->10 步长为1 默认1
print(j)
end
letters = {"a","b","c"} --声明数组
for i,v in ipairs(letters) do --ipairs函数迭代数组
print(i,v)
end
function max(a,b) --函数
if (a>=b)
then
return a
else
return b
end
end
print("the max is " .. max(5,9) .. " in 5 and 9")
function add(fix,...) --固定参数+可变数量参数传参
local sum = 0
print("fix-----"..fix)
print("the amount of params is : ",select("#",...)) --select # 获得传参的数量
print("params started from 3 :",select(3,...)) --从第三个参数开始到结束
for i,v in ipairs({select(3,...)})
do
print(i,v)
end
for i,v in ipairs({...}) --可变参数迭代
do
print(i,v)
sum=sum+v
end
return sum
end
print("sum: " .. add("fix",1,2,3,4,5))
function operation4(a)
return a+a*a/a+a%a --四则运算
end
print((operation4(2)~=4)) --判断相等 >= =< ==
print( 4==4 and operation4(3)==6 and (not (4<9))) -- and or not
print(not (4<9))
function stringOpe() --字符串截取
local test="Abcdefgh"
print(string.upper(test))
print(string.lower(test))
print (string.find(test,"bc")) --在字符串中找
print(string.gsub(test,"fgh","gggg",1)) --找到fgh 替换为gggg,替换1次
print(string.len(test))
print(string.sub(test,1,3)) --截取,lua从1开始数,从1到3默认截取到末尾
print(string.format("the value is %d",4))
print(string.format("the value is %s", "string"))
end
stringOpe()
function arrayOpe()
array={"a","b","c"} --在 Lua 索引值是以 1 为起始,但你也可以指定 0 开始。
for i=1,3
do
print(array[i]) --访问数组
end
end
arrayOpe()
function tableOpe() --表操作,可以方便的构造数组或者字典
myTable = {}
myTable[1] = 23
myTable[2] = "2222"
myTable["Chinese"] = "China"
print(string.format("the type of mytable[2] is %s",type(myTable[2])))
print(table.concat(myTable,"--",1,2)) --1到2的元素 用--连接起来
table.insert(myTable,3,"insert") --指定位置插入,默认末尾
print(string.format("the inserted value is %s " ,myTable[3]))
end
tableOpe()
q="q"
w="q"
KEYS={"Q","Q"}
print(q==w and KEYS[1] == KEYS[2] )
2 java中使用lua脚本 返回值不能为Integer
the returnType should be Long BOOLEAN or LIST but not Integer which throws IllegalStateException
版权声明:本文为weixin_42410730原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。