如何使用“下一个!”在Rails上循环? [英] How to use "Next!" on Rails for a loop?

查看:164
本文介绍了如何使用“下一个!”在Rails上循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致敬,

我是学习Rails(Ruby On Rails)计划的新手,我在其中一个课程中遇到了障碍。赋值让我创建一个奇数,脚本使用next组件从18到0开始读取。这是他们给我改变的例子:

I'm a newbie learning the Rails (Ruby On Rails) program and I've came to a road block on one of the lessons. The assignment has me creating an odd numbers for the script to read starting from "18 to 0" using the "next" component. This is the example they've given me to change :

i = 20
loop do
  i -= 1
  print "#{i}"
  break if i <= 0
end "

我想知道我是否应该在下一个创建一个模数,该模数将以i开头0或18开头?这将是这样的:

I wanted to know if I should the "next" to create a modulo that will begin with i leading to 0 or instead 18 ? It would be like this :

i = 20
loop do
  i -= 1
  next if i % 18 == 0
  print "#{i}"
  break if i <= 0
end




  • T-Boye

  • 推荐答案

    首先,什么是问题?如果问题是,如何跳过偶数,那么你几乎就在那里。你会做%2 而不是%18 但如果问题是接着做什么,你应该看看这里并向下滚动到下一个部分。

    First of all, what is the question? If the question is, how to skip even numbers, then you are almost there. You would do % 2 instead of % 18 though. If the question is "what does next do" you should look here and scroll down to the next section.

    建议:

    i = 19
    loop do
      i -= 1
      next if i % 2 == 1
      print i
      break if i <= 0
    end
    

    这篇关于如何使用“下一个!”在Rails上循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆