我是Ruby的新手,我正试图通过创建一个虚假的亲子鉴定来获得乐趣。我的代码是否适合将用户输入链接到已定义的数组?
array1 = [a,b,ab,o]
Print "mother blood type"
user_input1 = gets.chomp
if user_input1 != array1[]
puts "try again"
else
puts user_input1 = array1[]
end
end
我将编写如下代码:
array1 = %w(a b ab o)
puts "mother blood type"
user_input1 = "o"
# I have hard-code for testing,you can put user_input1 = gets.chomp
if array1.include? user_input1
puts user_input1
else
puts "try again"
end
# >> mother blood type
# >> o
在您的代码中,
array1 = [a,b,ab,o]
不是有效的数组。您可以将其写为%w(a b ab o)
或array1 = ['a','b','ab','o']
Print "mother blood type"
是错误的语句。不存在Print
,而存在print
。您的
if - end
块也无效。请参阅此处-Ruby If,Else If命令语法。