为什么这个 "deaf grandma" 程序不正确?
Why is this "deaf grandma" program incorrect?
此代码将 return 根据整个字符串是否大写 return 正确的短语
response = ''
while response != "BYE"
puts "What would you like deary?"
response = gets.chomp
if response = response.downcase
puts "speak up!"
else
puts "I can hear you!"
end
response = gets.chomp
end
然而,无论输入的字符串是全部小写还是全部大写,此代码总是 returns "speak up":
puts "What would you like deary?"
response = gets.chomp
while response != "BYE"
if response = response.downcase
puts "speak up!"
else
puts "I can hear you!"
end
response = gets.chomp
end
我有点困惑为什么会这样。
您似乎想要测试相等性 (==
),而不是执行赋值 (=
):
if response == response.downcase
此代码将 return 根据整个字符串是否大写 return 正确的短语
response = ''
while response != "BYE"
puts "What would you like deary?"
response = gets.chomp
if response = response.downcase
puts "speak up!"
else
puts "I can hear you!"
end
response = gets.chomp
end
然而,无论输入的字符串是全部小写还是全部大写,此代码总是 returns "speak up":
puts "What would you like deary?"
response = gets.chomp
while response != "BYE"
if response = response.downcase
puts "speak up!"
else
puts "I can hear you!"
end
response = gets.chomp
end
我有点困惑为什么会这样。
您似乎想要测试相等性 (==
),而不是执行赋值 (=
):
if response == response.downcase