我正在使用一個 android lua IDE 程式,我似乎找不到它在第 27 行給我這個“意外符號附近”錯誤的原因。請幫助它,非常感謝[1]:https: //i.stack.imgur.com/lcIsM.png
uj5u.com熱心網友回復:
看看 Lua 參考資料(我不懂語言):
if ...
then
do
bal = subtract(bal, 1000)
print("You lost")
end
elseif ...
then
bal = add(bal, 2000)
print("You won")
do
end
end
(縮進可能完全錯誤。)
uj5u.com熱心網友回復:
從你的截圖:
if v == 6 or 7 or 8 or 9 or 10
then bal = subtract(bal,1000)
then print("You lost the gamble! Your new balance is", bal")
elseif v == 1 or 2 or 3 or 4 or 5
then bal = add(bal, 2000)
then print("You won the gable! Your new balance is", bal)
end
問題 1:每個數字都是真值,因此無論v值是多少,v== 6 or 7 or 8 or 9 or 10都將始終決議為真。
你需要寫v == 6 or v == 7 or v == 8 or v == 9 or v == 10或干脆v >= 6 and v <= 10。
對于v == 1 or 2 or 3 or 4 or 5.
問題 2:條件陳述句的語法是if condition then body end. 你不能有額外的thens。
固定代碼:
if v == 6 or v == 7 or v == 8 or v == 9 or v == 10 then
bal = subtract(bal,1000)
print("You lost the gamble! Your new balance is", bal")
elseif v == 1 or v == 2 or v == 3 or v == 4 or v == 5 then
bal = add(bal, 2000)
print("You won the gable! Your new balance is", bal)
end
其他事宜:
為什么要有減法和加法功能?用函式代替基本的算術運算子是沒有意義的。只需使用bal = bal - 1000
由于 v 似乎是 1 到 10 之間的整數,為什么不簡單地v < 6用作獲勝條件?其余的可以用 else 來完成。您不需要明確檢查每個案例。
你的游戲也沒有太大意義。假設您在亂數生成器中使用均勻分布,則您贏了 2000 場比賽,輸了 1000 場比賽。因此,在很多場比賽中,您都可以保證贏的比輸的多。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/387438.html
