Why isn't this Ruby calculator working as intended?

血红的双手。 提交于 2019-12-12 02:57:25

问题


This code is intended to be a basic calculator with some features not yet added.

Whenever this is run the two beginning puts display, but when the program receives user input it closes.

Is there anything fundamentally wrong? I tried debugging for ~1 hour now and I have given up.

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:


You will need to put chomp at the end of the gets to strip the returned value:

gets.chomp

Then you will enter the "Distance" and the "Slope" you also need to watch for the case. You can also match everything with a downcase in your conditional statement to remove case sensitivity.



来源:https://stackoverflow.com/questions/25572500/why-isnt-this-ruby-calculator-working-as-intended

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!