Lua中算術(shù)運(yùn)算符的使用示例
更新時間:2015年05月27日 12:05:10 投稿:goldensun
這篇文章主要介紹了Lua中算術(shù)運(yùn)算符的使用示例,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
下表列出了所有的Lua語言支持的算術(shù)運(yùn)算符。假設(shè)變量A持有10和變量B持有20,則:

例子
試試下面的例子就明白了所有的Lua編程語言提供了算術(shù)運(yùn)算符:
復(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 )
當(dāng)執(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開發(fā)中實現(xiàn)MVC框架的簡單應(yīng)用
最近的游戲項目中使用了lua腳本來開發(fā),項目中用到了MVC框架,最近有朋友問我怎么弄,在這里簡單分享一下思路和一些開發(fā)中的技巧。有需要的小伙伴可以參考下。2015-04-04
Lua 數(shù)學(xué)庫的所有函數(shù)功能作用一覽
這篇文章主要介紹了Lua 數(shù)學(xué)庫的所有函數(shù)功能作用一覽,本文羅列了lua數(shù)學(xué)庫的所有函數(shù),并對每個函數(shù)的功能作用做了簡短描述,需要的朋友可以參考下2015-06-06
Lua中函數(shù)與面向?qū)ο缶幊痰幕A(chǔ)知識整理
函數(shù)在面對對象的編程中又被叫做方法,會受到作用域的制約,Lua中具有類等面向?qū)ο蟮奶匦?接下來我們就來看一下Lua中函數(shù)與面向?qū)ο缶幊痰幕A(chǔ)知識整理2016-06-06

