2014-10-20 109 views
-1

我该如何解决这个问题,运行时,它没有解决A,B,C,它只输出我输入的内容,例如,我输入3,B,5,它输出3,B,5,不是!3,4,5请注意,TRIG比的是不完整的,我只是复制他们在如何在这里正确使用if/then/else?

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() 
+0

谢谢你的快速回复,它的工作:) – 2014-10-20 06:24:17

回答

1
if B == "string" 

应该

if B == "B" 
0

另外一个可能的解决方案:

a = io.read() 

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

任何不是数字的输入(即使没有输入)也是如此。

这可能是你在那里试图做的。

0

尝试改变:

If A == "string" then 

随着

If type(A) == "string" then 

而且这样做也为B和C,它应该工作。