scala实现倒计时10秒摇号随机数1和2
以下是使用Scala实现倒计时10秒摇号随机数1和2的代码:
import scala.util.Random
import scala.concurrent.duration._
object RandomNumberGenerator extends App {
val random = new Random()
var count = 10
while (count > 0) {
val randomNumber = random.nextInt(2) + 1
println(s"Remaining Time: $count seconds, Random Number: $randomNumber")
count -= 1
Thread.sleep(1000)
}
}
这个程序会每秒输出剩余时间和一个随机数1或2,总共倒计时10秒。其中,random.nextInt(2) + 1生成一个1到2之间的随机数,Thread.sleep(1000)让程序暂停1秒钟。
原文地址: https://www.cveoy.top/t/topic/hQ5I 著作权归作者所有。请勿转载和采集!