Lua elseif 无法正常工作 [英] Lua elseif not working properly

查看:26
本文介绍了Lua elseif 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 elseif 时,它不起作用.在下面的代码中,无论用户输入哪个数字,唯一运行的代码就是 if 语句下的代码.

When I attempt to use elseif, it doesn't work. In the case of the code below, no matter which number the user inputs, the only code that runs is the code under the if statement.

io.write("do you want to convert from celsius to farenheit (1), or the other way around (2)?")
pref = io.read()
if pref == 1 then
  io.write("Hi there! what's your temperature in celsius? ")
  local cel = io.read()
  far = (cel*1.8+32)
  io.write("That temperature in farenheit is: " .. far)
elseif pref == 2 then
  io.write("Hi there! what's your temperature in farenheit? ")
  local far = io.read()
  cel = ((far-32)/1.8)
  print("That temperature in celsius is: " .. cel)
end

推荐答案

问题不在于 elseif.问题是因为 io.read() 返回一个字符串.要么将其转换为数字:

The problem is not with the elseif. The problem is because io.read() returns a string. Either convert it to a number:

pref = tonumber(io.read())

或者,将 pref"1""2" 进行比较.

Or, compare pref with "1", "2" instead.

这篇关于Lua elseif 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆