Ruby gets/puts only for strings?

前端 未结 4 1915
难免孤独
难免孤独 2020-12-09 10:07

I\'m new to Ruby and am currently working on some practice code which looks like the following:

puts \'Hello there, Can you tell me your favourite number?\'
         


        
4条回答
  •  甜味超标
    2020-12-09 10:41

    I wrote a similar program as yours. Here is how I finally got it to work properly! I had to assign the favorite number to be an integer. Then, in the next line I set the new_fav_num with the value of fav_num +1 and then converted it to string. After that, you can just plug your code into the return statement that you want to say to the user, only you have to convert the first fav_num to a string.

    puts "What is your favorite number?"
    
    fav_num = gets.chomp.to_i
    
    new_fav_num = (fav_num + 1).to_s
    
    puts "Your favorite number is " + fav_num.to_s + ". That's not bad, but " +
    new_fav_num + " is bigger and better!"

    Hope this helps.

提交回复
热议问题