LoginSignup
0
0

More than 3 years have passed since last update.

Ruby学習#11 Building a Calculator

Posted at

puts "Enter a number: "
num1 = gets.chomp()
puts "Enter another number"
num2 = gets.chomp()

puts (num1 + num2)

Enter a number
5
Enter another number
2
52
Stringなので、足し算にならない。解決にはto_i使う

puts "Enter a number: "
num1 = gets.chomp()
puts "Enter another number"
num2 = gets.chomp()

puts (num1.to_i + num2.to_i)

Enter a number
5
Enter another number
2
7

Enter a number
5
Enter another number
2.5
7

小数点以下認識しない。解決にはto_f使う
puts (num1.to_f + num2.to_f)

Enter a number
5
Enter another number
2.5
7.5
以下のほうがソース綺麗
puts "Enter a number: "
num1 = gets.chomp().to_f
puts "Enter another number"
num2 = gets.chomp().to_f

puts (num1 + num2)

Enter a number
5.5
Enter another number
2.1
7.6

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0