Decreasing for loop in Scala?

前端 未结 8 1023
一生所求
一生所求 2020-12-17 20:24

First day and first attempt at using Scala - so go easy on me! I\'m trying to rewrite some old Java code I have which is simply a function which takes two numbers and prints

8条回答
  •  别那么骄傲
    2020-12-17 21:26

    object Test extends App{
    
      def decrement(start: Int, finish: Int,dec :Int) = {
        for (i <- Range(start,finish,dec)) {
          println("Current value (decreasing from "+start+" to "+finish+") is "+i)
        }
      }
    
      decrement(5,0,-1)
    }
    

    This is also a method. but may not be the best

提交回复
热议问题