当前位置:网站首页>Scala IO case
Scala IO case
2022-06-24 23:38:00 【Jue Niu thunder plough hot blade】
import java.io.{
BufferedWriter, FileWriter}
import scala.collection.mutable.ListBuffer
import scala.io.Source
object StuGrade {
case class Student(var name: String,
var chinese: Int,
var math: Int,
var english: Int) {
def getSum: Int = chinese + math + english
}
def main(args: Array[String]): Unit = {
val source = Source.fromFile("./data/student.txt")
val stuArr: Iterator[Array[String]] = source.getLines().map(_.split(" "))
val stuList = ListBuffer[Student]()
// Traverse the acquired object , Encapsulate it as Student After the object , Add to List in
for (s <- stuArr) {
stuList += Student(s(0), s(1).toInt, s(2).toInt, s(3).toInt)
}
// Sort the list in descending order by total score
val sortList = stuList.sortBy(_.getSum).reverse.toList
val bw = new BufferedWriter(new FileWriter("./data/stu.txt"))
for (s <- sortList) {
bw.write(s"${s.name}\t${s.chinese}\t${s.math}\t${s.english}\t${s.getSum}")
bw.newLine()
}
bw.close()
source.close()
}
}
边栏推荐
- Hydropower project construction scheme based on 3D GIS Development
- 都2022年了,你还不了解什么是性能测试?
- 【UVM入门 ===> Episode_8 】~ Sequence 和 Sequencer、Sequence 层次化
- QT display RGB data
- js监听页面或元素scroll事件,滚动到底部或顶部
- Websocket learning
- Stm32f030f4 reading infrared remote control data
- Morris traversal
- Using ADC to control brushless motor source program STM32 library function
- Sword finger offer merges two sorted lists
猜你喜欢

QT cannot be edited with UTF-8

基于三维GIS开发的水电工程建设方案

7-7 digital triangle

Hydropower project construction scheme based on 3D GIS Development

选择类排序法

Sword finger offer merges two sorted lists

go 语言指针,值引用和指针引用

How to use stm32subeide SWV function

RT thread uses RT kprintf

Tiktok practice ~ upload and release app video
随机推荐
7-5 maximal submatrix sum problem
Design and practice of vivo server monitoring architecture
RT thread uses RT kprintf
Simple use of libnum Library (hexadecimal string conversion)
【UVM入门 ===> Episode_8 】~ Sequence 和 Sequencer、Sequence 层次化
Tremblement de terre réel ~ projet associé unicloud
7-5 最大子矩阵和问题
From client to server
Start QT program
R语言dplyr包select函数将dataframe数据中的指定数据列移动到dataframe数据列中的第一列(首列)
7-7 solving mode problems
Websocket long link pressure test
libnum库简单使用(进制字符串转换)
HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games
QT cannot be edited with UTF-8
MySQL semi sync replication
QT display RGB data
Basic data type
7-2 求解买股票问题
7-6 铺设油井管道