Hello , Ruby!

时间:2021-6-3 作者:qvyue

Question

Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Answer

程序中主要使用了

  • for i in start .. end循环结构,也可以用 times,while实现
  • if then else 选择结构
  • % 求余运算
  • and 与逻辑运算
  • 数组
  • 方法定义:def function_name(args,…) … end
# @param {Integer} n
# @return {String[]}
def fizz_buzz(n)
    result = Array.new()
    for i in 1 .. n
        if i % 3 == 0 and i % 5==0 then
          result 
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。