Lua中算術(shù)運(yùn)算符的使用示例
下表列出了所有的Lua語(yǔ)言支持的算術(shù)運(yùn)算符。假設(shè)變量A持有10和變量B持有20,則:
例子
試試下面的例子就明白了所有的Lua編程語(yǔ)言提供了算術(shù)運(yùn)算符:
b = 10
c = a + b
print("Line 1 - Value of c is ", c )
c = a - b
print("Line 2 - Value of c is ", c )
c = a * b
print("Line 3 - Value of c is ", c )
c = a / b
print("Line 4 - Value of c is ", c )
c = a % b
print("Line 5 - Value of c is ", c )
c = a^2
print("Line 6 - Value of c is ", c )
c = -a
print("Line 7 - Value of c is ", c )
當(dāng)執(zhí)行上面的程序,它會(huì)產(chǎn)生以下結(jié)果:
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2.1
Line 5 - Value of c is 1
Line 6 - Value of c is 441
Line 7 - Value of c is -21
相關(guān)文章
分析Lua觀察者模式最佳實(shí)踐之構(gòu)建事件分發(fā)系統(tǒng)
當(dāng)對(duì)象間存在一對(duì)多關(guān)系時(shí),則使用觀察者模式(Observer Pattern)。比如,當(dāng)一個(gè)對(duì)象被修改時(shí),則會(huì)自動(dòng)通知依賴它的對(duì)象。觀察者模式屬于行為型模式2021-06-06Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實(shí)例
這篇文章主要介紹了Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實(shí)例,本文給出了加法、減法、乘法、除法、相反數(shù)、取模等內(nèi)容,需要的朋友可以參考下2014-09-09Lua的迭代器使用中應(yīng)該避免的問(wèn)題和技巧
這篇文章主要介紹了Lua的迭代器使用中應(yīng)該避免的問(wèn)題和技巧,本文介紹了避免創(chuàng)建閉合函數(shù)、利用恒定狀態(tài)創(chuàng)造更多變量、不需要for循環(huán)的迭代器等內(nèi)容,需要的朋友可以參考下2014-09-09Lua學(xué)習(xí)筆記之函數(shù)、變長(zhǎng)參數(shù)、closure(閉包)、select等
這篇文章主要介紹了Lua學(xué)習(xí)筆記之函數(shù)、變長(zhǎng)參數(shù)、closure(閉包)、select等,本文著重講解了這些特性的用法,并給出代碼實(shí)例,需要的朋友可以參考下2015-04-04