2014-09-19 64 views
0

我试图从附加到字符串的方法得到的整数串随机整数,我已尝试多种不同的事情:<<.concat(虽然相同)+=以及 我所选择以使该方法由于我的工作将要在 后来加了功能与此代码的问题是不是在等回my string + firstrandomnumber + secondrandomnumber所以它只返回my string + latestrandomnumber附加到使用.concat

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 
+1

也许你应该移动'slotThread ='你的号码是:'while循环之外? – August 2014-09-19 00:13:02

+0

好吧,这是尴尬.. 谢谢你的意见tho – user2010884 2014-09-19 00:16:59

+0

嗨@ user2010884是我的回答有帮助吗? – Anthony 2014-09-19 14:16:29

回答

0

改变这两条线:

slotThread.concat(machineSlot.to_s) 
    #if x was entered the slotThread appends a random number 

    puts slotThread 
    #slotThreads current process 

puts "#{slotThread}#{machineSlot}" 

这就是所谓的串插,一些使用了很多的红宝石。

顺便说一下,您正在使用CamelCase命名您的变量和方法,这不是约定。不紧急,但我有时间检查出this