如何正确使用if / then / else?

时间:2014-10-20 06:07:21

标签: lua

如何解决这个问题,当它运行时,它不能解决A,B,C,它只输出我输入的内容,例如,我输入3,B,5,它输出3,B,5,不是3,4,5!请注意,触发比率不完整,我只是复制了它们。

print("Welcome to my right triangle solver")
print("What is side A?Enter A if unknown!")
A=io.read()
print("What is side B?Enter B if unknown!")
B=io.read()
print("What is side C?Enter C if unknown!")
C=io.read()
print("What is angle a?Enter a if unknown!")
a=io.read()
print("What is angle b?Enter b if unknown!")
b=io.read()
print("What is angle c?Enter c if unknown!")
c=io.read()
if A == "string" then
  A=math.sqrt(C^(2)-B^(2))
elseif B == "string" then
  B=math.sqrt(C^(2)-A^(2))
elseif C == "string" then
  C=math.sqrt(A^(2)+B^(2))
end
print("Great!")
print("Calculating")
print("Side A is=" .. A)
print("Side B is=" .. B)
print("Side C is=" .. C)
print("Side a is=" .. a)
print("Side b is=" .. b)
print("Side c is=" .. c)
print("done")
print("What is your name? ")
name=io.read()
print("Thank you," .. name)
print("How was your day?")
day=io.read()
print("mine too,bye!")
io.read()

3 个答案:

答案 0 :(得分:1)

if B == "string"

应该是

if B == "B"

答案 1 :(得分:0)

也是一种可能的解决方案:

a = io.read()

if not tonumber(a) then
    --do stuff
end

任何不是数字的输入(甚至没有输入)都是真的。

这可能就是你在那里尝试做的事情。

答案 2 :(得分:0)

尝试改变:

If A == "string" then

使用

If type(A) == "string" then

对B和C也这样做,它应该有效。