代码中的while替换为for循环怎么写
import scala.collection.mutable.ArrayBuffer
import util.control.Breaks._
/**
* 随机生成10个不重复的[50,100)范围内的数字,存入一个可变数组中。
*/
object problem4 {
def main(args: Array[String]): Unit = {
val arrayBuffer: ArrayBuffer[Int] = ArrayBuffer[Int]()
var i: Int = 0
while (i < 10) {
val num: Int = (Math.random() * 50 + 50).toInt
breakable {
for (j <- arrayBuffer.indices) {
if (num == arrayBuffer(j)) {
break
}
}
arrayBuffer += num
i += 1
}
}
println(arrayBuffer.mkString(","))
}
}
完全可以用for循环替代