减少 Scala 中的 for 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9976955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decreasing for loop in Scala?
提问by rwb
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 out the numbers from x to y. For example, i have the increment function:
第一天和第一次尝试使用 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)
}
}
However, im struggling writting a corresponding decrement function which will decrease from start to finish? I have read Scala downwards or decreasing for loop?but am still unsure
但是,我正在努力编写一个相应的递减函数,该函数会从头到尾递减?我已经向下阅读Scala 或减少 for 循环?但我仍然不确定
Thank you
谢谢
回答by dacwe
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
回答by user unknown
for (i <- (6 to 3 by -1)) {println ("i: " + i)}
i: 6
i: 5
i: 4
i: 3
If you happen to forget by -1, you can move up and use a function, to revert the result:
如果你碰巧忘记了by -1,你可以向上移动并使用一个函数来恢复结果:
for (i <- (3 to 6)) {println ("i: " + ((6+3) - i))}
To exclude the second boundary, use until:
要排除第二个边界,请使用until:
for (i <- (6 until 3 by -1)) {println ("i: " + i)}
i: 6
i: 5
i: 4
Alternatively, you could define an Iterator for your purpose. Extending an Iterator is easy; just implement 'hasNext:Boolean' and 'Next:[T]', where T is the type to handle - in our case Int or maybe Long or BigInt:
或者,您可以为您的目的定义一个迭代器。扩展迭代器很容易;只需实现 '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)
回答by Roman A. Taycher
highnum to lownum by -1 (switch with other negative or positive step to change stepping)
highnum 到 lownum 由 -1(切换与其他负或正步骤以更改步进)
def decrement(start: Int, finish: Int) = {
for (i <- start to finish by -1) {
println("Current value (decreasing from "+start+" to "+finish+") is "+i)
}
}
I think this is a dupe of Scala downwards or decreasing for loop?
我认为这是Scala 向下或减少 for 循环的欺骗?
回答by Nilesh Shinde
This way you can use Decreasing for loop in Scala.
这样您就可以在 Scala 中使用递减 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
回答by FedUpWithPowerHungryHumans
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
这也是一种方法。但可能不是最好的
回答by Azam Khan
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")
}
}
method call:
方法调用:
printInDecreasingOrder(10, 2)
printInDecreasingOrder(10, 2)
Result:
结果:
Current value (decreasing from 10 to 2) is 10
当前值(从 10 减少到 2)为 10
Current value (decreasing from 10 to 2) is 9
当前值(从 10 减少到 2)是 9
Current value (decreasing from 10 to 2) is 8
当前值(从 10 减少到 2)为 8
Current value (decreasing from 10 to 2) is 7
当前值(从 10 减少到 2)为 7
Current value (decreasing from 10 to 2) is 6
当前值(从 10 减少到 2)是 6
Current value (decreasing from 10 to 2) is 5
当前值(从 10 减少到 2)是 5
Current value (decreasing from 10 to 2) is 4
当前值(从 10 减少到 2)为 4
Current value (decreasing from 10 to 2) is 3
当前值(从 10 减少到 2)是 3
Current value (decreasing from 10 to 2) is 2
当前值(从 10 减少到 2)为 2
printInDecreasingOrder(1, 10)
printInDecreasingOrder(1, 10)
Result:
结果:
first num is smaller than second
第一个数字小于第二个
回答by Christopher Chiche
Here is a global increment/decrement solution inspired by Scala downwards or decreasing for loop?:
这是受Scala启发的全局增量/减量解决方案向下或减少 for 循环?:
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)
}
}

