scala实现彩票算法
(1)具体实现代码如下:
package hw1
import scala.util.control._
/**
* @author BIGDATA
*/
object Cp {
var input=new Array[Int](7)
var cp=new Array[Int](7)
var level=0
def main(args:Array[String]):Unit={
//var num=readInt
buy()
printUsr();
kaijiang();
printzhongJ();
duijiang();
fajiang();
}
def buy():Unit={
println("请输入1-35之间的七个整数:")
var i=0
while(i<7)
{
input(i)=readInt
if(input(i)>=1 && input(i)<=35)
{
val loop = new Breaks
loop.breakable {
for(j<- 0 to i-1)
{
if(input(i)==input(j))
{
println("输入重复,请重新输入:")
i-=1
loop.break()
}
}
}
i+=1
}
else
{
println("输入数据越界,请重新输入:")
i-=1
}
}
}
def printUsr():Unit={
println("用户所买的号码是:")
for(i<- 0 to 6) print(input(i)+" ")
println()
}
def kaijiang():Unit={
var i=0
while(i<7)
{
cp(i)=(math.random*35+1).toInt
val loop = new Breaks
loop.breakable {
for (j <- 0 to i-1)
{
if (cp(i)==cp(j))
{
i-=1
loop.break()
}
}
}
i+=1
}
}
def printzhongJ():Unit={
println("系统中将号码为:")
for(i<- 0 to 6) print(cp(i)+" ")
println()
}
def duijiang():Unit={
printf("用户中奖等级为:")
for(i<- 0 to 6)
{
val loop1 = new Breaks
loop1.breakable {
for(j <- 0 to 6)
{
if(input(i)==cp(j))
{
level+=1
loop1.break()
}
}
}
}
}
def fajiang():Unit={
level match{
case 7 => println("1等奖")
case 6 => println("2等奖")
case 5 => println("3等奖")
case 4 => println("4等奖");
case 3 => println("5等奖")
case 2 => println("6等奖")
case 1 => println("7等奖");
case 0 => println("好遗憾,没有中奖!")
}
}
}
(2)运行结果如下: