** Day 13 of Calendar Planning 2020 ** It's been about 3 months since I started studying programming, so I will leave a note of what I learned in the article as an output. I would be happy if I could help anyone entering the world of programming. Please let me know if there are any words that are wrong, wrong, or misunderstood ^^ I'm sorry if it's hard to read the words for a long time. I will do my best to get used to it little by little.
I will try to create it again for personal review!
--If you pass a number divisible by 3 as an argument, [Fizz] is returned. --If you pass a number divisible by 5 as an argument, [Buzz] is returned. --If you pass a number divisible by both 3 and 5 as an argument, it returns [FizzBuzz]. --In other cases, return the argument as a character string
This time, use the gets method to enable input to the keyboard (excluding non-numeric input)
demo_fizz_buzz.rb
def fizz_buzz(number)
if number % 15 == 0
"FizzBuzz"
elsif number % 3 == 0
"Fizz"
elsif number % 5 == 0
"Buzz"
else
number.to_s
end
end
puts "Please enter a number"
input = gets.to_i
puts "Result is! ??"
puts fizz_buzz(number)
It should look like this! (There may be other answers!)
fizz_buzz (number)
Set the method name and arguments.
Use the if statement to clear the condition.
The method is completed by describing the return value for the condition as a process! !!
Don't forget to perform type conversion that meets the conditions properly.
First, write the condition that "both 3 and 5 are divisible" in the contents of the if statement! The reason is the order of processing! Since it is processed in order from the top If you write the condition that "3 or 5 can be broken" first, the condition that "both can be broken" will not be caught at all ^^;
You can complete it if you pay attention only here!
When I didn't understand it yet, I was worried that I couldn't complete it because I made a mistake in this order ... ^^;
Recommended Posts