使用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?但仍不确定
谢谢
答案 0 :(得分:16)
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
答案 1 :(得分:4)
for (i <- (6 to 3 by -1)) {println ("i: " + i)}
i: 6
i: 5
i: 4
i: 3
如果你碰巧忘了by -1
,你可以向上移动并使用一个函数来恢复结果:
for (i <- (3 to 6)) {println ("i: " + ((6+3) - i))}
要排除第二个边界,请使用until
:
for (i <- (6 until 3 by -1)) {println ("i: " + i)}
i: 6
i: 5
i: 4
或者,您可以为您的目的定义迭代器。扩展迭代器很容易;只需实现'hasNext:Boolean'和'Next:[T]',其中T是要处理的类型 - 在我们的例子中是Int或者Long或BigInt:
class FromToIterator (start: Int, stop: Int) extends Iterator [Int] {
var current = start
// 3 6 3 6 6 3 6 3
def hasNext : Boolean = ((start < stop && current <= stop) || (start > stop && current >= stop))
def next: Int = {
val res = current
if (start < stop) current += 1 else current -= 1
res
}
}
val it = new FromToIterator (3, 6)
val ti = new FromToIterator (6, 3)
for (i <-it) println (i)
for (i <-ti) println (i)
答案 2 :(得分:1)
highnum to lownum by -1(切换为其他负步或正步以改变步进)
def decrement(start: Int, finish: Int) = {
for (i <- start to finish by -1) {
println("Current value (decreasing from "+start+" to "+finish+") is "+i)
}
}
的欺骗
答案 3 :(得分:1)
这样您可以在Scala中使用Decreasing for循环。
object Example extends App {
for(i <- 20 to 2 by -2){
println("Value of i = "+ i)
}
}
------------------
O/P
------------------
Value of i = 20
Value of i = 18
Value of i = 16
Value of i = 14
Value of i = 12
Value of i = 10
Value of i = 8
Value of i = 6
Value of i = 4
Value of i = 2
答案 4 :(得分:0)
这是一个受Scala downwards or decreasing for loop?启发的全局增量/减量解决方案:
def goThrough(start: Int, finish: Int) = {
val d = if(start<=finish) 1 else -1
for (i <- start to finish by d) {
println("Current value (increasing from "+start+" to "+finish+") is "+i)
}
}
答案 5 :(得分:0)
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)
}
这也是一种方法。但可能不是最好的
答案 6 :(得分:0)
def printInDecreasingOrder(start : Int, end : Int){
if(start > end ){
for(i <- start to end by -1){
println(s"Current value (decreasing from $start to $end) is $i")
}
}else{
println("first num is smaller than second")
}
}
方法调用:
printInDecreasingOrder(10, 2)
结果:
当前值(从10减少到2)是10
当前值(从10减少到2)是9
当前值(从10减少到2)是8
当前值(从10减少到2)是7
当前值(从10减少到2)是6
当前值(从10减少到2)是5
当前值(从10减少到2)是4
当前值(从10减少到2)是3
当前值(从10减少到2)是2
printInDecreasingOrder(1, 10)
结果:
第一个num小于第二个
答案 7 :(得分:0)
如果您要执行此操作,则可以添加到此答案中
for(j <- finish to start){......}
sbt甚至不显示错误。因此,如果您想递减for循环,则需要这样做
for(j <- finish to start by -1){.......}