为什么这个Ruby计算器不按预期工作?

时间:2014-08-29 16:36:56

标签: ruby calculator

此代码旨在成为尚未添加某些功能的基本计算器。然而,每当这个运行时,2开始显示。但是当程序收到任何用户输入时它关闭的那一刻。有什么有趣的错误吗?我现在已经尝试了约1小时,我已经放弃了。 (我对编程也很新,所以任何提示都会受到赞赏)

puts "Hello There. What you you like to calculate?"
puts "You have the option of Distance, Slope, Pythag, Fibonacci, and Sales Tax"
calculationSelection = gets

if calculationSelection == "Distance"
puts "You have selected Distance"
puts "Please enter x2"
x2 = gets
puts "Please enter x1"
x1 = gets
puts "Please enter y2"
y2 = gets
puts "Please enter y1"
y1 = gets

Distance = Math.sqrt((x2.to_f - x1.to_f)**2 + (y2.to_f - y1.to_f)**2)
print "Your distance is: "
puts Distance.to_s
pause
elsif calculationSelection == "Slope"
puts "You have selected Slope"
puts "Enter x of Point One"
x1 = gets
puts "Enter y of Point One"
y1 = gets
puts "Enter x of Point Two"
x2 = gets
puts "Enter y of Point Two"
y2 = gets

Slope = (x1.to_f - x2.to_f) / (y1.to_f - y2.to_f)
print "Your slope is: "
puts Slope.to_s 
pause
end

1 个答案:

答案 0 :(得分:1)

你需要把' .chomp'在结束时剥夺了回报......

gets.chomp

然后你将进入"距离"和" Slope"你还需要注意这个案子。您还可以使用" .downcase"来匹配所有内容。在条件语句中删除区分大小写。