-- Example 11 -- Numbers. -- Multiple assignment showing different number format. -- Two dots (..) are used to concatenate strings (or a -- string and a number). a,b,c,d,e = 1, 1.123. 1E9, -123, .0008 print("a="..a, "b="..b, "c="..c, "d="..d, "e="..e) ------ Output ------ a=1 b=1.123 c=1000000000 d=-123 e=0.0008 -- 例子 11 -- 数字 -- 以下多个赋值语句说明了不同的数字格式。 -- 两个点号被用来连接字符串(也可连接一个字符串和一个数字) -- Example 12 -- More Output. -- More writing output. print "Hello from Lua!" print("Hello from Lua!") ------ Output ------ Hello from Lua! Hello from Lua! -- 例子 12 -- 更多的输出 -- Example 13 -- More Output. -- io.write writes to stdout but without new line. io.write("Hello from Lua!") io.write("Hello from Lua!") -- Use an empty print to write a single new line. print() ------ Output ------- Hello from Lua!Hello from Lua! -- 例子13 -- 更多的输出 -- io.write可以写控制台,但不会换行 -- print()可以打印一个空行 -- Example 14 -- Tables. -- Simple table creation. a={} -- {} creates an empty table b={1,2,3} -- creates a table containing numbers 1,2,3 c={"a","b","c"} -- creates a table containing strings a,b,c print(a,b,c) -- tables don't print directly, we'll get back to this!! ------- Output ------- table:009EB468 table:009EB558 table:009EB580 -- 例子 14 -- 表 -- 简单的创建表 a={} -- {}创建一个空表 b={1,2,3} -- 创建一个包含数字1,2,3的表 c={"a","b","c"} -- 创建一个包含串a,b,c的表 print(a,b,c) -- 不能直接的打印表,我们会回来搞定这个问题!! -- Example 15 -- More Tables. -- Associate index style. address={} -- empty address address.Street="Wyman Street" address.StreetNumber=360 address.AptNumber="2a" address.City="Watertown" address.State="Vermont" address.Country="USA" print(address.StreetNumber, address["AptNumber"]) ------ Output ------ 360 2a -- 例子 15 -- 表续集 -- 关联,索引风格 -- Example 16 -- if statement. -- Simple if. a=1 if a == 1 then print(a is one) end ------ Output ------- a is one -- 例子 16 -- if语句 -- 简单的if -- Example 17 -- if else statement. b="happy" if b=="sad" then print("b is sad") else print("b is not sad") end ------ Output ------ b is not sad -- 例子 17 -- if else 语句 -- Example 18 -- if elseif else statement c=3 if c==1 then print("c is 1") elseif c==2 then print("c is 2") else print("c isn't 1 or 2, c is "..tostring(c)) end ------ Output ------- c isn't 1 or 2, c is 3 -- Example 19 -- Conditional assignment. -- value = test and x or y a=1 b=(a==1) and "one" or "not one" print(b) -- is equivalent to a=1 if a==1 then b = "one" else b= "not one" end print(b) ------ Output ------ one one -- 例子 19 -- 条件赋值 -- Example 20 -- while statement a=1 while a~=5 do -- Lua uses ~= to mean not equal a=a+1 io.write(a.." ") end ------ Output ------- 2 3 4 5 -- 例子20 -- while语句 -- Lua使用~=来表示不等于