+ 3
Ruby Calculator using input?
How do you build a calculator in Ruby using an input and assuming the first character of the input is the integer and the second character is the operator and the third character is the second integer?
5 ответов
+ 9
I just read this and niwo is right
+ 2
You could use the .to_f method on a string to transform a string to a float(so floating point numbers will work to...). Use the .chomp method to remove the line break from the input, so you get the numbers. Your Program could start like:
num1 = gets.chomp.to_f
opr = gets.chomp
num2 = gets.chomp.to_f
After that, you switch between the operands you want to use. E.g.:
if opr=="+"
puts num1+num2
elsif opr=="-"
...
...
...
end
+ 1
yes, like I have written a week ago...👍😂