出现错误,即使我是数字,我也无法将 nil 与数字进行比较
Got an error that I can't compare nil to number even though I is a number
我正在尝试做一个简单的冒泡排序(我的代码可能看起来不专业,但我只是在学习)并且在我的冒泡排序函数中我得到一个错误代码说我无法将 nil 与数字进行比较,即使所有我的 table 中的值应该是数字。
local function bubblesort(array)
for getal = 1, #array do
for i = 1, #array do
if array[i] > array[i + 1] then
local temp = array[i]
array[i + 1] = array[i]
array[i + 1] = temp
end
end
end
return array
end
如果您需要查看它,这里是制作 table 并打印出来的其余代码
local function printtabel(tabel)
for _,v in pairs(tabel) do
io.write(v..' ')
end
print()
end
io.write("Geef seed, grootte en max: ")
local SEED = io.read("*n")
local GROOTTE = io.read("*n")
local MAX = io.read("*n")
math.randomseed(SEED)
local t = {}
for n = 1,GROOTTE do
t[n] = math.random(1,MAX)
end
bubblesort(t)
printtabel(t)
我得到的错误在第 5 行,
if array[i] > array[i + 1] then
如果你做这个比较,当 i
是数组的最后一个索引时会发生什么?那么 array[i + 1]
将在数组之外,或者在 lua 的情况下 nil
值。
您可以通过更改 for 循环的结束值来解决此问题:
for i = 1, #array - 1 do
if array[i] > array[i + 1] then
local temp = array[i]
array[i + 1] = array[i]
array[i + 1] = temp
end
end
也与你的错误有点无关,但你排序的逻辑似乎是错误的。
这段代码只使用了 array[i]
值而不是 array[i + 1]
local temp = array[i]
array[i + 1] = array[i]
array[i + 1] = temp
我认为这些可能是您由于错误而尚未解决的问题
I get an error code saying I can't compare nil to number even though all the values in my table should be numbers.
这实际上是有道理的,因为 table 中的 nil 值相当于该值根本不存在。您可能会认为 tables 充满了无限数量的 nils。
问题出在这里:
for i = 1, #array do
if array[i] > array[i + 1] then
最后的i
等于#array
,因此array[i + 1]
在数组外。
I get an error code saying I can't compare nil to number even though all the values in my table should be numbers
好吧,计算机不只是编造东西,所以它是试图比较数字和零。如果您的 table 中的所有值都是数字,则它从某处获取 nil 而不是您的 table.
中的值之一
当试图找出如此简单的错误时,创建一个非常小的样本并打印输出以确保循环内的一切都符合您的预期会很有帮助。您可以点击this在线查看和运行示例代码。结果:
i = 1, array[i] = 2
i+1 = 2, array[i+1] = 5
i = 2, array[i] = 5
i+1 = 3, array[i+1] = 1
i = 3, array[i] = 5
i+1 = 4, array[i+1] = nil
/var/task/bin/lua: main.lua:6: attempt to compare nil with number
所以你可以看到你的功能很大。示例数组中有三个值,您将 i 从 1 循环到 3,但您在循环中将 array[i]
与 array[i+1]
进行比较。这意味着当 i 为 3 时,您正在与 array[4]
进行比较。在 lua 中,您可以访问数组长度之外的元素,但它们 return nil.
只需使用简单的输入或添加 print 语句来逐句查看代码以查看实际使用的内容,这是调试未按您认为应有的方式工作的代码的好方法。
我正在尝试做一个简单的冒泡排序(我的代码可能看起来不专业,但我只是在学习)并且在我的冒泡排序函数中我得到一个错误代码说我无法将 nil 与数字进行比较,即使所有我的 table 中的值应该是数字。
local function bubblesort(array)
for getal = 1, #array do
for i = 1, #array do
if array[i] > array[i + 1] then
local temp = array[i]
array[i + 1] = array[i]
array[i + 1] = temp
end
end
end
return array
end
如果您需要查看它,这里是制作 table 并打印出来的其余代码
local function printtabel(tabel)
for _,v in pairs(tabel) do
io.write(v..' ')
end
print()
end
io.write("Geef seed, grootte en max: ")
local SEED = io.read("*n")
local GROOTTE = io.read("*n")
local MAX = io.read("*n")
math.randomseed(SEED)
local t = {}
for n = 1,GROOTTE do
t[n] = math.random(1,MAX)
end
bubblesort(t)
printtabel(t)
我得到的错误在第 5 行,
if array[i] > array[i + 1] then
如果你做这个比较,当 i
是数组的最后一个索引时会发生什么?那么 array[i + 1]
将在数组之外,或者在 lua 的情况下 nil
值。
您可以通过更改 for 循环的结束值来解决此问题:
for i = 1, #array - 1 do
if array[i] > array[i + 1] then
local temp = array[i]
array[i + 1] = array[i]
array[i + 1] = temp
end
end
也与你的错误有点无关,但你排序的逻辑似乎是错误的。
这段代码只使用了 array[i]
值而不是 array[i + 1]
local temp = array[i]
array[i + 1] = array[i]
array[i + 1] = temp
我认为这些可能是您由于错误而尚未解决的问题
I get an error code saying I can't compare nil to number even though all the values in my table should be numbers.
这实际上是有道理的,因为 table 中的 nil 值相当于该值根本不存在。您可能会认为 tables 充满了无限数量的 nils。
问题出在这里:
for i = 1, #array do
if array[i] > array[i + 1] then
最后的i
等于#array
,因此array[i + 1]
在数组外。
I get an error code saying I can't compare nil to number even though all the values in my table should be numbers
好吧,计算机不只是编造东西,所以它是试图比较数字和零。如果您的 table 中的所有值都是数字,则它从某处获取 nil 而不是您的 table.
中的值之一当试图找出如此简单的错误时,创建一个非常小的样本并打印输出以确保循环内的一切都符合您的预期会很有帮助。您可以点击this在线查看和运行示例代码。结果:
i = 1, array[i] = 2
i+1 = 2, array[i+1] = 5
i = 2, array[i] = 5
i+1 = 3, array[i+1] = 1
i = 3, array[i] = 5
i+1 = 4, array[i+1] = nil
/var/task/bin/lua: main.lua:6: attempt to compare nil with number
所以你可以看到你的功能很大。示例数组中有三个值,您将 i 从 1 循环到 3,但您在循环中将 array[i]
与 array[i+1]
进行比较。这意味着当 i 为 3 时,您正在与 array[4]
进行比较。在 lua 中,您可以访问数组长度之外的元素,但它们 return nil.
只需使用简单的输入或添加 print 语句来逐句查看代码以查看实际使用的内容,这是调试未按您认为应有的方式工作的代码的好方法。