加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

减少Scala中的循环?

发布时间:2020-12-16 09:03:42 所属栏目:安全 来源:网络整理
导读:第一天和第一次尝试使用 Scala – 所以对我来说很容易!我正在尝试重写一些旧的 Java代码,它只是一个函数,它接受两个数字并打印出从x到y的数字.例如,我有增量功能: def increment(start: Int,finish: Int) = { for (i - start to finish) { println("Curren
第一天和第一次尝试使用 Scala – 所以对我来说很容易!我正在尝试重写一些旧的 Java代码,它只是一个函数,它接受两个数字并打印出从x到y的数字.例如,我有增量功能:

def increment(start: Int,finish: Int) = {
      for (i <- start to finish) {
         println("Current value (increasing from "+start+" to "+finish+") is "+i)
      }
    }

然而,我正在努力写一个相应的减量函数,从开始到结束将减少?我读过Scala downwards or decreasing for loop?但仍然不确定

谢谢

解决方法

scala>def decrement(start: Int,finish: Int) = {
    |  for (i <- start to finish by -1)
    |   println("Current value (decreasing from "+start+" to "+finish+") is "+i);
    | }
decrement: (start: Int,finish: Int)Unit

scala> decrement(10,1)
Current value (decreasing from 10 to 1) is 10
Current value (decreasing from 10 to 1) is 9
Current value (decreasing from 10 to 1) is 8
Current value (decreasing from 10 to 1) is 7
Current value (decreasing from 10 to 1) is 6
Current value (decreasing from 10 to 1) is 5
Current value (decreasing from 10 to 1) is 4
Current value (decreasing from 10 to 1) is 3
Current value (decreasing from 10 to 1) is 2
Current value (decreasing from 10 to 1) is 1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读