Lua中算術(shù)運算符的使用示例
更新時間:2015年05月27日 12:05:10 投稿:goldensun
這篇文章主要介紹了Lua中算術(shù)運算符的使用示例,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
下表列出了所有的Lua語言支持的算術(shù)運算符。假設(shè)變量A持有10和變量B持有20,則:
例子
試試下面的例子就明白了所有的Lua編程語言提供了算術(shù)運算符:
復(fù)制代碼 代碼如下:
a = 21
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 )
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 )
當執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:
復(fù)制代碼 代碼如下:
Line 1 - Value of c is 31
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
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觀察者模式最佳實踐之構(gòu)建事件分發(fā)系統(tǒng)
當對象間存在一對多關(guān)系時,則使用觀察者模式(Observer Pattern)。比如,當一個對象被修改時,則會自動通知依賴它的對象。觀察者模式屬于行為型模式2021-06-06Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實例
這篇文章主要介紹了Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實例,本文給出了加法、減法、乘法、除法、相反數(shù)、取模等內(nèi)容,需要的朋友可以參考下2014-09-09Lua學(xué)習(xí)筆記之函數(shù)、變長參數(shù)、closure(閉包)、select等
這篇文章主要介紹了Lua學(xué)習(xí)筆記之函數(shù)、變長參數(shù)、closure(閉包)、select等,本文著重講解了這些特性的用法,并給出代碼實例,需要的朋友可以參考下2015-04-04