我的lua代码有什么问题?

时间:2017-04-07 10:27:30

标签: lua

local consumer = coroutine.create(function(i)
   while true do
    i = i - 1
    print('consumer: ' .. i)
    coroutine.yield(i)
   end
end)

local producer = coroutine.create(function()
   while true do
     i = i or 0
     i = i + 1
     print('producer: ' .. i)
     status, value = coroutine.resume(consumer, i)
     i = value
   end
end)

coroutine.resume(producer)

the console print like this

但这是完全错误的,正确的印刷应该是这样的: 0 1 0 1 01

1 个答案:

答案 0 :(得分:0)

在使用者中将coroutine.yield(i)替换为i = coroutine.yield(i)。 @EgorSkriptunoff感谢您的帮助