Lua的五种变量类型、局部变量、全局变量 、lua运算符 、流程控制if语句

  • Lua代码的注释方式:

--当行注释

--[[    多行注释    ]]--

  • Lua的5种变量类型:

  1.null 表示空数据 等同于null
  2.boolean 布尔类型 存储true 和false
  3.String 字符串类型,字符串可以用双引号也可以用单引号表示
  4.number小数类型(lua中没有整数类型?
  5.table类型
    myTable = {34.31.30}
    myTable[3]                                              注意 Lua中的索引是从1开始的。
  可以用type()来取得一个变量的类型

  • 全局变量和局部变量

  默认定义的变量都为全局变量,定义局部变量需要在前面加一个local 。

  在代码块中声明的为局部变量,当代码块运行结束的时候,这个变量则会被释放

  • lua中运算符

  1.算术运算符+-*/%(lua中没++ -- 这样的运算符)

  2.关系运算符 <= < > >= ==

  3.逻辑运算符  and or not  分别表示与 或 非  (类似于C#中的 &&  || !)

     -- and 运算符的使用 (下面为特殊的用法)

    -- 如果第一个表达式为 true ,而 第二个表达式的运算结果是一个非布尔的值,则输出这个值
    print(26<27 and 3)   --3
    print(true and 4)    --4

   -- or 运算符的使用 (下面为特殊的用法)

    --如果第一个表达式为false ,而第二个表达式的运算结果是一个非布尔型的值,则输出这个值
    print(1>2 or 5)    --5

   -- 非 not     在lua中所有不是 false 和 nil 的值都代表 true
   -- 连接  ..  (用于连接两个字符串 )       eg: print(“521”.."1234") -- 5211234

  • Lua的流程控制if语句

  1.if 表达式  then

    语句块

   end

  2.if 表达式  then

    语句块

    else

    语句块

    end

  3.if 表达式 then

    语句块

  elseif  表达式  then

    语句块

  else

    语句块

   end

下面是 if  例子

-- 一个数 90-100 优秀  70-90 良好  60-70 及格   小于60大于100  不及格
local mathaa =
if <=mathaa and mathaa<= then
print ("优秀")
elseif <= mathaa and mathaa <= then
print("良好")
elseif <=mathaa and mathaa<= then
print("及格")
else
print("不及格")
end
--*********************************************************************************************
--写一个脚本 判断一个数是否为大于100的偶数
local shu =
if shu % == and shu > then
print("此数为一个偶数")
else
print("此数不是一个偶数")
end
--**********************************************************************************************
--写一个脚本判断两个数是否 都为 小于或等于 200的奇数 (不能被2整除的数)
local a ,b = ,
if a%== and a<= and b<= and b%== then
print ("ab 两个数都为小于等于200的奇数")
elseif a%== and a<= then
print ("a为小于等于200的奇数")
elseif b<= and b%== then
print ("B为小于等于200的奇数")
else
print ("ab 两个数都不是小于等于200的奇数")
end

最新文章

  1. SQL Server 2008 R2没有卸载干净
  2. ubuntu编译最新版本WebKit
  3. vsftpd 搭建与介绍
  4. Spring Boot笔记(一)
  5. WPF布局容器综合展示
  6. HDU1372:Knight Moves(经典BFS题)
  7. UESTC_方老师和农场 2015 UESTC Training for Graph Theory&lt;Problem L&gt;
  8. MC- 挂单STOP交易
  9. javascript学习笔记(一):词法结构
  10. Django中模板的用法简介
  11. echarts (geo/map) 渐变效果
  12. IntelliJ IDEA 中 Web项目 目录结构
  13. 可遇不可求的Question之MySqlClient的Guid 类型的映射篇
  14. window.opener()方法
  15. python图片处理(一)
  16. jeecg框架解决跨域问题
  17. 【DataGuard】部署Data Guard相关参数详解 (转载)
  18. 【JDF】学习和理解
  19. C / C ++中的数组讲解
  20. 015-GenericEncodingFilter模板【解决全局乱码】

热门文章

  1. LeetCode258——Add Digits
  2. Android游戏框架Libgdx使用入门
  3. Atitit.一个cms有多少少扩展点,多少api&amp;#160;wordpress&amp;#160;&amp;#160;cms有多少api。。扩展点
  4. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly&#39;s Chemicals
  5. P1052 过河(状态压缩)
  6. C++ Web 编程(菜鸟教程)
  7. C# WinForm小程序(技术改变世界-cnblog)
  8. codeforces round #424 div2
  9. (Go)04.go工作区目录规范及简单例子
  10. codevs1519 过路费(最小生成树+LCA)