在 Ruby 中使用 If 和 Else 语句
Using If and Else statements in Ruby
对编程世界非常陌生,刚刚开始学习,通过 Flatiron School 的预习工作并且一直做得很好,但由于某种原因无法理解 "if" 和 "else" 语句。该问题类似于 Chris Pine 'deaf grandma' 问题,但没有说 "BYE!" 三遍。
~该方法应该接受一个包含短语的字符串参数,并检查该短语是否全部大写:如果不是,那么奶奶听不到你的声音。然后她应该回应 (return) HUH?!大声说,桑尼!
~但是,如果你对她大喊大叫(即使用包含全大写短语的字符串参数调用该方法,那么她可以听到你的声音(或者至少她认为她可以)并且应该回应( return) 不,不是自 1938 年以来!
我到目前为止:
def speak_to_grandma
puts "Hi Nana, how are you?".upcase
if false
puts "HUH?! SPEAK UP, SONNY!"
else
puts "NO, NOT SINCE 1938!"
end
end
但是我得到了错误的参数数量...我应该如何在使用 if/else 语句时添加参数?这可能是一个非常简单和基本的问题,但我似乎无法理解这个问题(可能是想太多了)。
任何帮助和清晰度将不胜感激。
input_phrase = "Hi Nana, how are you?"
def speak_to_grandma(phrase)
# Check if string equals same phrase all upper case letters, which means string is all uppercase
if phrase == phrase.upcase
# return this string if condition is true
puts "NO, NOT SINCE 1938!"
else
# return this string if condition is false
puts "HUH?! SPEAK UP, SONNY!"
end
end
# execute function passing input_phrase variable as argument
speak_to_grandma(input_phrase)
how am I supposed to add argument while using the if/else statements?
This is probably a very easy and basic question but can't seem to get
my head around this (overthinking probably).
你的错误是函数没有接受任何参数,这里它接受 "phrase" 变量作为参数并处理它:
def speak_to_grandma(phrase)
你有
if false
但没有检查到底什么是假的..用"false"重写我的版本:
input_phrase = "Hi Nana, how are you?"
def speak_to_grandma(phrase)
# Check if it is false that string is all upper case
if (phrase == phrase.upcase) == false
# return this string if condition is false
puts "HUH?! SPEAK UP, SONNY!"
else
# return this string if condition is true
puts "NO, NOT SINCE 1938!"
end
end
speak_to_grandma(input_phrase)
这里我在评价
if (phrase == phrase.upcase) == false
基本意思是"if expression that phrase equals phrase all uppercase is false"
对编程世界非常陌生,刚刚开始学习,通过 Flatiron School 的预习工作并且一直做得很好,但由于某种原因无法理解 "if" 和 "else" 语句。该问题类似于 Chris Pine 'deaf grandma' 问题,但没有说 "BYE!" 三遍。
~该方法应该接受一个包含短语的字符串参数,并检查该短语是否全部大写:如果不是,那么奶奶听不到你的声音。然后她应该回应 (return) HUH?!大声说,桑尼!
~但是,如果你对她大喊大叫(即使用包含全大写短语的字符串参数调用该方法,那么她可以听到你的声音(或者至少她认为她可以)并且应该回应( return) 不,不是自 1938 年以来!
我到目前为止:
def speak_to_grandma
puts "Hi Nana, how are you?".upcase
if false
puts "HUH?! SPEAK UP, SONNY!"
else
puts "NO, NOT SINCE 1938!"
end
end
但是我得到了错误的参数数量...我应该如何在使用 if/else 语句时添加参数?这可能是一个非常简单和基本的问题,但我似乎无法理解这个问题(可能是想太多了)。
任何帮助和清晰度将不胜感激。
input_phrase = "Hi Nana, how are you?"
def speak_to_grandma(phrase)
# Check if string equals same phrase all upper case letters, which means string is all uppercase
if phrase == phrase.upcase
# return this string if condition is true
puts "NO, NOT SINCE 1938!"
else
# return this string if condition is false
puts "HUH?! SPEAK UP, SONNY!"
end
end
# execute function passing input_phrase variable as argument
speak_to_grandma(input_phrase)
how am I supposed to add argument while using the if/else statements? This is probably a very easy and basic question but can't seem to get my head around this (overthinking probably).
你的错误是函数没有接受任何参数,这里它接受 "phrase" 变量作为参数并处理它:
def speak_to_grandma(phrase)
你有
if false
但没有检查到底什么是假的..用"false"重写我的版本:
input_phrase = "Hi Nana, how are you?"
def speak_to_grandma(phrase)
# Check if it is false that string is all upper case
if (phrase == phrase.upcase) == false
# return this string if condition is false
puts "HUH?! SPEAK UP, SONNY!"
else
# return this string if condition is true
puts "NO, NOT SINCE 1938!"
end
end
speak_to_grandma(input_phrase)
这里我在评价
if (phrase == phrase.upcase) == false
基本意思是"if expression that phrase equals phrase all uppercase is false"