首页 技术 正文
技术 2022年11月18日
0 收藏 634 点赞 3,617 浏览 1392 个字

前言

Lua有6中数据类型,分别是nil(空)、boolean(布尔)、number(数字)、string(字符)、table(表)、function(函数)

在Lua中可以使用type函数来返回一个值或者变量所属的类型,如:

print(type("helle world"))-->output:string
print(type(print))-->output:function
print(type(true))-->output:boolean

1.nil(空)

  Lua将nil用来表示“无效值”。一个变量再第一次赋值前默认值是nil,将nil赋值给一个全局变量就等同于删除它。

local num
print(num)-->output:nil
num = 100
print(num)-->output:100
num = nil            print(num)             -->output:nil

2.boolean(布尔)

  布尔值可选值为true/false,在Lua中nil和false为“假”,其余都为“真”,比如0和空字符都为真。

3.number(数字)

  number用于表示实数,可以使用数学函数math.floor(向下取整),math.ceil(向上取整)

localorder=3.0
localscore=98.5
print(math.floor(order))-->output:3
print(math.ceil(score))-->output:99

4.string(字符)

  在Lua中,字符串有三种表示方法:

  1)使用一对单引号。如:‘hello’

  2)使用一对双引号。如:“hello”

  3)使用长括号(即[[]])来定义。

  注:Lua的字符串中的转移字符不起作用。

    Lua的字符串是不可改变的值,不能像再c语言中那样直接修改字符串的某个字符,而是根据修改要求来创建一个新的字符串。

    Lua也不能通过下标来访问字符串的某个字符。

localstr3 = [["add\name",'hello']]
localstr4 = [=[stringhavea[[]].]=]
print(str1)-->output:helloworld
print(str2)-->output:hellolua
print(str3)-->output:"add\name",'hello'
print(str4)-->output:string have a [[]].

5.table(表)

  table类型实现了一种抽象的“关联数组”。关联数组是一种具有特殊索引方式的数组,索引通常是字符串或者number类型,但也可以是除nil以外任意类型的值。

6.function(函数)

  在Lua中,函数也是一种数据类型,函数可以存储在变量中,可以通过参数传递给其他函数,还可以作为其他函数的返回值。

  函数以end结尾。

local function foo()
  print("in the function")
  --dosomething()
  local x = 10
  local y = 20
  return x + y
end
local a = foo--把函数赋给变量
print(a())
--output:
in the function
30

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774