问题
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