ComputerCraft Catch 函数在 while 循环中

ComputerCraft Catch function during while loop

我在 ComputerCraft 中创建了一个 "false" OS,它允许我从头开始构建我系统的用户可以做什么和不能做什么。

然而,我似乎无法做的一件事是让这个 OS 在解析不存在的 "program" 时给出有效的回溯。

例如,我创建了程序"Help, Admin, Database" 我想要的是每当我输入这 3 个程序以外的东西时,就会出现 "Program does not exist" 之类的错误。

这个 LUA OS 的构建是为了让命令行界面看起来尽可能干净,而不是效率。这个 OS 是在 3 天的时间内建成的,所以失去我的位置是很常见的,因此有些事情并不完全平行。

这是我的脚本OS,它会在启动 ComputerCraft 计算机时立即运行

--Testing Comments--

--Program Start--
term.clear()            //This deletes the CraftOS 1.7 watermark//
term.setCursorPos(1,1)
print("KappaCraft Interface: 2018 Edition")
print("")
sleep(.75)
print("Welcome to the Information Center!")
sleep(.75)
print("")
print("This system is not yet finished")   //You're telling me//
print("")
sleep(.75)
print("Contact the Author of this program, Eleventy49, for any further questions")
sleep(2.0)

--Capabilities--
print("")
print("Type 'Commands' to see what you can do.")
sleep(.75)
print("")

--Setting up for Programs--
varyable = "true"
while varyable == "true" do
local input = read()

--Programs--
if input == "Commands" then
term.clear
term.setCursorPos(1,1)
print("This is the list of programs available to you")
print("")
print("Admin  Commands  Database  Help  Building Code")
print("")
sleep(2)
end

--Admin--
if input == "Admin" then
term.clear
term.setCursorPos(1,1)
write("Enter Password: ")
local passinput = read("*")
if passinput == "Classified" then
error("Adminstrator Access Granted")   //This error forces me back into CraftOS, so I can edit this Script//
sleep(2)
term.clear()
else
print("Must be Administrator to Access this Program")
sleep(2)
end
end

--Builder's Code--
if input == "Building Code" then
term.clear()
term.setCursorPos(1,1)
shell.run("Builder")
print("The Builder's Code has been printed on your right")  //Builder is a program that formats the building rules of the village onto computer craft printer sheets and then prints them//
end

--Help--
in input == "Help" then
term.clear()
term.setCursorPos(1,1)
print("Help with what? (EX: Help Commands)")
end

--Help Admin--
if input == "Help Admin" then
term.clear
term.setCursorPos(1,1)
print("This program lets Admins change settings")
end

--Help Commands--
if input == "Help Commands" then
term.clear
term.setCursorPos(1,1)
print("This program displays all the programs installed on this computer")
end

--Help Help--
if input == "Help Help" then
term.clear
term.setCursorPos(1,1)
print("This program explains what other programs do")

--Help Database--
if input == "Help Database" then
term.clear
term.setCursorPos(1,1)
print("This program lists all the plots and their owners in the city")
end

--Help Building Code--
if input== "Help Building Code" then
term.clear
term.setCursorPos(1,1)
print("This program prints the Building Code using the printer")
end

--Help Shutdown--
if input == "Shutdown" then
term.clear
term.setCursorPos(1,1)
print("This program shuts down the computer")
end

--Database--
if input == "Database" then
term.clear
term.setCursorPos(1,1)
print("This program is not yet complete")
end

--Shutdown--
if input == "Shutdown" then
term.clear
term.setCursorPos(1,1)
print("As you wish")
sleep(1)
os.shutdown()
end

end

不使用所有 if 个块,而是使用 if/elseif/else 个块。参见 here。因此,您可以对所有接受的命令使用 ifelseif,然后使用最终的 else 打印错误消息。