2016-07-26 51 views
-5

我在做第11個練習瞭解Ruby的難法。我一定要得到這樣的:通過寫這Gets.chomp不能正常工作

How old are you? 35 
How tall are you? 6'2" 
How much do you weigh? 180lbs 
So, you're 35 old, 6'2" tall and 180lbs heavy. 

print "How old are you? " 
age = gets.chomp() 
print "How tall are you? " 
height = gets.chomp() 
print "How much do you weigh? " 
weight = gets.chomp() 

puts "So, you're #{age} old, #{height} tall and #{weight} heavy." 

,但我得到這個:

How old are you? 
How tall are you? 
How much do you weigh? 
So, you're old, tall and heavy. 

這意味着,gets.chomp不工作,我不明白任何東西。

+0

方法'gets.chomp()'在哪裏?這似乎要麼沒有返回,要麼可能是其他問題......? – gravity

+0

@gravity'Kernel#gets'和'String#chomp'是Ruby核心方法。 –

+0

@MladenJablanović這是我的觀點:) – gravity

回答

4

你應該輸入值,而不僅僅是按回車鍵,當運行程序後,系統提示:

# ⇓ prompt ⇓ ⇓⇓ YOUR INPUT!!! 
How old are you? 35 

旁註chomp後面的括號中是多餘的,而不是紅寶石地道。

+0

哦,謝謝。這很簡單。 –