Issue
you can see the problem here
I started programming in lua and I wanted to make a simple program. the problem I’m experiencing is that if I enter if functions within the code the program won’t start and auto ends at startup
as you can see from image the compiler said "terminated"
update: now the code looks like this
local function main()
print("insert four binary number for color encryption")
a,b,c,d = io.read("*n","*n","*n","*n")
a=n
b=n
c=n
d=n
end
main()
if(a == 1){
print(white)
}
then
print(black)
end
i moved the if under the main() at the end
now the console gives nil
follow by
Exception in thread "main" com.naef.jnlua.LuaRuntimeException: ...t-win32.win32.x86_64\workspace\luabello\src\main.lua:10: attempt to call a boolean value
at com.naef.jnlua.LuaState.lua_pcall(Native Method)
at com.naef.jnlua.LuaState.call(LuaState.java:555)
at org.eclipse.ldt.support.lua51.internal.interpreter.JNLua51Launcher.run(JNLua51Launcher.java:128)
at org.eclipse.ldt.support.lua51.internal.interpreter.JNLua51Launcher.main(JNLua51Launcher.java:143)
update 2: now the code looks like this
local function main()
print("insert four binary number for color encryption")
a,b,c,d = io.read("*n","*n","*n","*n")
end
main()
a=n
b=n
c=n
d=n
if(a <= 1){
print(white)
}
then
else
print(black)
end
it gave me another error
Exception in thread "main" com.naef.jnlua.LuaRuntimeException: ...t-win32.win32.x86_64\workspace\luabello\src\main.lua:11: attempt to compare nil with number
at com.naef.jnlua.LuaState.lua_pcall(Native Method)
at com.naef.jnlua.LuaState.call(LuaState.java:555)
at org.eclipse.ldt.support.lua51.internal.interpreter.JNLua51Launcher.run(JNLua51Launcher.java:128)
at org.eclipse.ldt.support.lua51.internal.interpreter.JNLua51Launcher.main(JNLua51Launcher.java:143)
Solution
your if condition is probably not what you expect it to be,
if(a == 1){
print(white)
}
this will attempt to call the result of a == 1
and pass for the first param {print(white)}
then evaluate if that is true, but you will get an error as you can not call the result of a == 1
as it is a boolean value.
I believe you meant:
if(a == 1) then
print(white)
else
print(black)
end
Also you are setting a,b,c,d
twice,
- to the result of
io.read("*n","*n","*n","*n")
- to
n
which is not defined so the values are set tonil
Answered By - Nifim