如何在这里正确使用if/then/else?

如何解决这个问题?运行时它不能计算A、B、C的值,它只输出我输入的内容,例如我输入3,B,5,它输出的是3,B,5,而不是3,4,5!请注意,三角函数比例不完整,我只是复制它们进去了。

print("欢迎来到我的直角三角形求解器")
print("A边是多少?如果未知请输入A!")
A = io.read()
print("B边是多少?如果未知请输入B!")
B = io.read()
print("C边是多少?如果未知请输入C!")
C = io.read()
print("角a是多少?如果未知请输入a!")
a = io.read()
print("角b是多少?如果未知请输入b!")
b = io.read()
print("角c是多少?如果未知请输入c!")
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("好的!")
print("正在计算")
print("A边长度=" .. A)
print("B边长度=" .. B)
print("C边长度=" .. C)
print("角a=" .. a)
print("角b=" .. b)
print("角c=" .. c)
print("完成")
print("你叫什么名字?")
name = io.read()
print("谢谢你," .. name)
print("你今天过得怎么样?")
day = io.read()
print("我也是,再见!")
io.read()
点赞
用户4323
用户4323

如果 B 等于 "string"

应该改为

如果 B 等于 "B"

2014-10-20 06:11:09
用户4173441
用户4173441

以下是可能的解决方案:

a = io.read()

if not tonumber(a) then
    --做一些事情
end

任何输入(甚至没有输入)都不是一个数字是true。

这可能就是您想在那里做的事情。

2014-10-23 12:35:38
用户4178917
用户4178917

尝试更改:

If A == "string" then

If type(A) == "string" then

并且也对 B 和 C 进行相同操作,它应该能够工作。

2014-10-24 18:49:54