2013-03-07 77 views
1

我剛開始學習Ruby。我正在創建一個簡單的程序,但我仍然不確定如何使用它。這是我的代碼:

=begin 
Filename: GameCompanyPoem.rb 
Program Name: Poem Generator 
Version 1.0 
Created: March 7th, 2013 
Purpose: To generate a poem based on key words given by the user 
=end 
print 'The following program will ask you to input words. Based on the words you input it will generate a poem. Now, please enter a name. ' 
name = gets.chomp 
print 'Please enter a mammal. ' 
mammal = gets.chomp 
print 'Now please enter a colour. ' 
colour1 = gets.chomp 
print 'We are almost done! Please enter a place or location now. ' 
place = gets.chomp 
print 'Now enter a verb. ' 
verb1 = gets.chomp 
print 'And another verb. ' 
verb2 = gets.chomp 
print 'Your poem will now be generated. Press enter to continue. 
space = gets.chomp 
print 'Someone by the name of ' + name + 'had a little' + mammal + ', little ' + mammal + ', little ' + mammal + ',' + name + 'had a little ' + mammal + 'its fleece was' + colour + 'as snow. It followed her to ' + place + 'one day, ' + place + 'one day, ' + place + 'one day, it made the children ' + verb1 + 'and ' + verb2 + 'to see a ' + mammal + 'at' + place + '."' 

當我運行它,在最後的方法(打印),每一個空間都有這樣的錯誤:

意外tIDENtifier,期待$結束

在每一個空間,如果我填補它,然後就不見了,但它進入到下一格

+0

閉引號上缺少''你的詩現在將產生。按enter鍵繼續。「語法突出顯示器使其更加明顯。 – 2013-03-07 18:20:17

+0

並且在最後一個字符串末尾有一個額外的'''(雙引號)。請參閱我的回答以獲得完整修復 – BlackHatSamurai 2013-03-07 18:21:51

+0

,它應該是coulor1,而不是顏色,就像@limelights提到的。 – BlackHatSamurai 2013-03-07 18:24:05

回答

1

嘗試:

print 'Your poem will now be generated. Press enter to continue.' 
print 'Someone by the name of ' + name + 'had a little' + mammal + ', little ' + mammal + ', little ' + mammal + ',' + name + 'had a little ' + mammal + 'its fleece was' + colour1 + 'as snow. It followed her to ' + place + 'one day, ' + place + 'one day, ' + place + 'one day, it made the children ' + verb1 + 'and ' + verb2 + 'to see a ' + mammal + 'at' + place + '.' 
+1

另外,他錯過了變量'colour',它應該是'colour1'!:) – 2013-03-07 18:22:40

+0

好的接收!已添加。謝謝! – BlackHatSamurai 2013-03-07 18:23:40

+0

感謝您的幫助!我修正了顏色變量,發現我在某處丟失了一個撇號那裏:) – user2145566 2013-03-07 18:27:59

2

一個NIFT y在包括Ruby在內的很多語言中使用技巧插入

print "Someone by the name of #{name} had a little #{mammal}, little #{mammal}, little #{mammal}, had a little #{mammal} its fleece was #{colour} as snow. It followed her to #{place} one day, #{place} one day, #{place} one day, it made the children #{verb1} and #{verb2} to see a #{mammal} at #{place}." 

嗯,它可能不會再押韻了。

還有一點。查看Ruby Interpolation瞭解詳細信息。但基本上ruby運行到#{something},它將其替換爲某個結果。

print "1 + 2 = #{1 + 2}"將打印

1 + 2 = 3

相關問題