2014-09-19 73 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