次のコードを実行しようとしていますが、私が必要とする応答が得られません。私は、クライアントの詳細ハッシュで子どもへの答えを設定したい。私はかなり私の子供のメソッドでget.chompがあるので、私はかなり確信していますが、私は私のハッシュにそれをシャベルすることができます私は整数にその応答を設定することはできません。ここで作成しようとしているRubyメソッドに問題があります
# Get following details from client:
# name
# age
# number of children
# decor theme
# handicap
# if handicap ask if they would like wheelchair ramp or elevator.
# Program should also:
# Print the hash back to screen when designer answers all questions
# Give the user the option to update a key. Exit if user says "n." If user enters "key," program should ask for new key value and update key. (Strings have methods to turn them into symbols.)
# Print latest version of hash, exit
def children(response)
until response == "y" || response == "n"
puts "Invalid response, Please enter Y/N only!"
response = gets.chomp.downcase
end
if response == "y"
puts "How many children do you have?"
number_of_children = gets.chomp.to_i
else response == "n"
number_of_children = 0
end
end
client_details = {}
# ---- Driver Code ----
puts "Welcome to the Client Information Form!"
# ---- get the user name ----
puts "Please enter the Clients Full Name:"
client_details[:name] = gets.chomp
# ---- get the user age ----
puts "Please enter #{client_details[:name]} age:"
client_details[:age] = gets.chomp.to_i
# ---- find out if the user has children ----
puts "Does #{client_details[:name]} have any children? (Please enter Y/N only)"
children_input = gets.chomp.downcase.to_s
children(children_input)
client_details[:children] = children(children_input)
p client_details
は、私は、コードを実行したときに何が起こるかです:
'Welcome to the Client Information Form!
Please enter the Clients Full Name:
Art V
Please enter Art V age:
55
Does Art V have any children? (Please enter Y/N only)
y
How many children do you have?
8
How many children do you have?
8
{:name=>"Art V", :age=>55, :children=>8}
問題が発生した場合は、問題の診断と解決策の提案に役立ちます。それがなければ、私たちができることはすべて推測です。 –
その間に、この文に問題があります。 'else number_of_children ==" n "' sytaxは従っていません。 "else"はスタンドアロン、条件なし、またはCONDITION〜elsif CONDITION〜else DEFAULTACTIONの場合に使用できます。 IF-ELSIF-ELSEの[link](http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/)を参照してください。 –
@CaptainChaos私はお詫び申し上げます。それはちょうど質問を二度尋ねます:----------クライアント情報フォームへようこそ! お客様の氏名を入力してください: アートV アートビズに入力してください:アートVは子供がいますか? (Y/Nのみを入力してください) y 子供は何人ですか? あなたは何人の子供がいますか? {:name => "アートV"、:年齢=> 55、:子供=> 8} – SG17