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

查看:212
本文介绍了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 12代替。

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

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