简体   繁体   中英

Gets.chomp input comparison returning an error

I AM USING RUBY

ERROR: comparison of Integer with String failed (ArgumentError)

puts "Age: "
    age = gets.chomp
    if 0 < age < 130

I want the programm to allow the user to input all the numbers between 0 (not included) and 130 (included). How to do it?

The input is a string. Try something like this

puts "Age: "
user_input = gets.chomp
begin
  age = Integer(user_input)
  # your code
rescue ArgumentError
  puts "Age must be an integer"
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM