简体   繁体   English

什么是int(input())的Lua等价物?

[英]What is the Lua equivalent of int(input())?

I'm asking the user for a number using 我要求用户输入一个号码

var = io.read()

and then when I do 然后当我这样做

if var == 1 then
    print ("Thing One")
elseif var == 2 then
    print ("Thing Two")
else
    print ("Thing Three")

This always returns "Thing Three" I am presuming this means I need var to get an integer, like when python uses int(input()) 这总是返回“Thing Three”我假设这意味着我需要var来获取一个整数,就像python使用int(input())

Lua supports an argument to io.read() call. Lua支持io.read()调用的参数。 You can pass *n or *number as a string to it so that your input would be a number or nil . 您可以将*n*number作为字符串传递给它,以便输入数字或数字nil

print "enter a number:"
n = io.read("*number")
if not n then error("invalid input") end

You should also take a light reading of this page of PiL . 您还应该仔细阅读本页的PiL


So, your code shall be: 所以,你的代码应该是:

var = io.read( "*n" ) -- or io.read( "*number" )

if var == 1 then
    print ("Thing One")
elseif var == 2 then
    print ("Thing Two")
else
    print ("Thing Three")
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM