gpt4 book ai didi

if-statement - Lua 'end' 预计关闭 'if'

转载 作者:行者123 更新时间:2023-12-03 11:11:12 24 4
gpt4 key购买 nike

我在 lua 中有这段代码

function onMouseDown(event)
print(event.x, event.y)
if event.x>160 then
print("what")
return 1
print("whatwhat")
else
return 0
print("WhatWhatWhat")
end


fruit:setX(event.x)
fruit:setY(event.y)
end

但它正在提示 main.lua:59: 'end' 预期(在第 56 行关闭 'if')在 'print' 附近,所以它期待在我的 if 下面 print("whatwhat")。但我在那里结束了,如果我这样做,它仍然会提示我。

我对 lua 很陌生,我有点困惑,我通常用 c# 编程

最佳答案

参见 Lua 手册 §3.3.4 :

The return statement can only be written as the last statement of a block. If it is really necessary to return in the middle of a block, then an explicit inner block can be used, as in the idiom do return end, because now return is the last statement in its (inner) block.

您的代码在 return 语句之后调用了 print,因此无效。

关于if-statement - Lua 'end' 预计关闭 'if',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com