日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lua 初接触 --- The first time use Lua for programing

發布時間:2024/1/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lua 初接触 --- The first time use Lua for programing 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

  The first time use Lua for programing?

Wang Xiao?

?

  1. 關于 lua 的變量類型:

   ?lua 變量的定義與matlab有點不同:

?


?

    local d , f = 5 ,10 --聲明局部變量 d,f。

    d , f = 5, 10; ?--聲明全局變量 d,f。
    d, f = 10 ? ? ? ?--[[聲明全局變量 d,f,其中 f 的值是 nil--]]

?


?

   ?如果只是定義沒有初始化,則靜態存儲變量被隱式初始化為 nil。

  Lua 賦值時會將第一個值賦給第一個變量,第二個值賦給第二個變量,依次類推。

  來一個比較叼的交換語句:

?


?

    local a,b ? ?-- 變量定義

    a = 10 ? ? b = 30

    print("value of a:", a)

    print("value of b:", b)

    b,a = a,b ? ?-- 直接將a and b 對換,有點叼有木有,連中間變量都不需要的。

    print("value of a:", a)

    print("value of b:", b)

    f = 70.0/3.0

    print("value of f", f)

?


  

  2. 兩個需要注意的地方: 

   ?若想要跳出循環,只要 break 語句即可。

  


  3. 函數的定義:

  --[[ function returning the max between two numbers --]]

  function max(num1, num2)

    if(num1 > num2) then?

      result = num1;

    else

      result = num2;

    end

    return result;

  end

?

  -- 調用函數

  print("The maximum of the two numbers is", max(10, 4))

  print("The maximum of the two numbers is", max(5, 6))

?

  -- 執行結果

  The maximum of the two numbers is ?10

  The maximum of the two numbers is ?6

?

?

   ?在 Lua 中,使用 ... 作為參數可以創建參數個數可變的函數,即: 變參函數。

  

  


  4. 字符串

  string1 = "Lua"

  print("\"String 1 is\"", string1)

  string2 = 'Tutorial'

  print("String 2 is", string2)

?

  string3 = [[ "Lua Tutorial" ]]

  print("String 3 is", string3)

?

  -- the output results

  "String 1" is ? ?Lua

  String 2 is?  Tutorial

  String 3 is?  "Lua Tutorial"

?

  

  

?

?

  -- the output are:

  Basic formating Lua Tutorial?

  Date formating 02/01/2014

  0.3333

  


  5. 數組:

  Lua 的索引是從1開始的。

?? 

  多維數組:

?

  array = {"Lua", "Tutorial"}?

  function elementlterator (collection)

    local index = 0

    local count = #collection

    -- 返回閉包函數

    return function?

      index = index + 1

      if index <= count?

      then?

        -- 返回迭代器的當前元素

        return collection[index]

      end

     ? ?end

    end

?

  for element in elementlterator(array)

  do?

    print(element)

  end

?


?

  

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

  

?

  

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的lua 初接触 --- The first time use Lua for programing的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。