自学内容网 自学内容网

在scala中完成成绩排名问题 | 创建包,创建类,导入文件

package test1

import scala.collection.mutable.ListBuffer

 

object Test1111 {

def main(args: Array[String]): Unit = {

case class Student(

name: String,

zongfen: Int,

var mingci: Int

)

val list = ListBuffer[Student]()

//并列

list += Student("孙涛", 90, 0) //4

 

list += Student("林峰", 102, 0) //2

list += Student("周周", 101, 0) //3

list += Student("郑选", 105, 0) //1

list += Student("陈晨", 90, 0) //4

list += Student("吴刚", 102, 0) //2

 

//对list碱性排序,按总分排名

val orderList = list.sortWith((a, b) => a.zongfen > b.zongfen)

 

//对有序排列,从前开始向后一一比较:

//(1)如果当前的分数不等于预设分数,则名次+1.更新预设分数

//(2)若当前的分数等于预设的分数,则名次不变

var curScore = -1

var mingci = 0;

for (elem <- orderList) {

if (elem.zongfen != curScore) {

mingci += 1

elem.mingci = mingci

curScore = elem.zongfen

} else {

elem.mingci = mingci

}

 

}

// orderList.foreach(println)

// println("-"*40)

list.foreach(println)

 

}

}


原文地址:https://blog.csdn.net/2301_81527420/article/details/144413204

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!