我试图将从方法中获取的整数追加到字符串中,我尝试了多种不同的东西:<<
和.concat
(尽管相同)+=
好
我选择使用我正在处理的函数来创建该方法,稍后将添加该函数
此代码的问题在于,它不会返回my string + firstrandomnumber + secondrandomnumber
,而只返回my string + latestrandomnumber
def machineSlot()
tal1= rand(0..10)
return tal1
end
#makes the a random number
if startBool == true
#game startof
gameRunner=true
puts 'pull the lever with x'
leverPullTry =gets.chomp
while gameRunner
#keeps game running
i=1
slotThread='Your numbers are:'
#initialise game rollcount i and string that keeps the numbers
if leverPullTry=='x'
slotThread.concat( machineSlot.to_s)
#if x was entered the slotThread appends a random number
puts slotThread
#slotThreads current process
puts 'pull the lever with x again'
i+=1
#number of rolls increased
leverPullTry =gets.chomp
else
puts 'try again use type x to pull'
leverPullTry =gets.chomp
#user didnt manage to input anything correct
end
end
end
答案 0 :(得分:0)
更改这两行:
slotThread.concat( machineSlot.to_s)
#if x was entered the slotThread appends a random number
puts slotThread
#slotThreads current process
到
puts "#{slotThread}#{machineSlot}"
这就是所谓的字符串插值,在ruby中使用了很多东西。
顺便说一句,您使用CamelCase命名变量和方法,这不是常规。不紧急,但有空的时候我会查看this。