九、Spark基础环境实战(下)

打开虚拟机,连接Xshell后

1. 编辑添加内容

往里面添加:

import scala.collection.mutable.ListBuffer

import scala.io.StdIn

object TodoManagerNe extends App {

private val todos = ListBufferString()

println("=== 待办事项管理器 ===")

println("命令列表:") println(" add 事项 - 添加待办事项")

println(" list - 查看所有待办事项")

println(" delete 序号 - 删除指定待办事项")

println(" exit - 退出程序")

println()

var running = true

while (running) {

print("> ")

val input = StdIn.readLine().trim.split("\\s+", 2)

if (input.isEmpty) {

// 空输入,忽略

} else {

input(0) match {

case "add" if input.length > 1 =>

todos += input(1)

println(s"已添加: ${input(1)}")

case "list" =>

if (todos.isEmpty) {

println("没有待办事项")

} else {

println("待办事项:")

todos.zipWithIndex.foreach { case (todo, index) =>

println(s" {index + 1}. todo")

}

}

case "delete" if input.length > 1 =>

try {

val index = input(1).toInt - 1

if (index >= 0 && index < todos.length) {

val removed = todos.remove(index)

println(s"已删除: $removed")

} else {

println("无效的序号")

}

} catch {

case _: NumberFormatException =>

println("请输入有效的数字序号")

}

case "exit" =>

running = false

println("再见!")

case _ =>

println("未知命令,请重试")

}

}

}

}

2. Scala 源代码文件(.scala)编译为 Java 字节码文件(.class)

scala TodoManagerNe.scala

  1. 运行已编译好的 Scala 程序
相关推荐
swipe10 小时前
从 0 到 1 实现大文件上传:分片、秒传、断点续传、暂停、重试与服务端合并
前端·javascript·面试
kyriewen11 小时前
AI 生成的代码能跑就行?这 5 个坑迟早炸
前端·javascript·ai编程
SelectDB11 小时前
秒级弹性、最高降本 70%:SelectDB Serverless 如何重塑云数仓资源效率
大数据·后端·云原生
kisshyshy11 小时前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
WhoAmI11 小时前
MapReduce框架原理解析一:InputFormat
大数据·hadoop
WhoAmI11 小时前
MapReduce框架原理解析三:OutputFormat
大数据·hadoop
胡志辉11 小时前
从v8源码和react深入浅出理解 JavaScript 作用域链与闭包
前端·javascript
WhoAmI12 小时前
MapReduce框架原理解析二:Shuffle
大数据·hadoop
Bolt12 小时前
TypeScript 7.0 来了:当 tsc 用 Go 重写之后
javascript·typescript·go
阳火锅14 小时前
😭测试小姐姐终于不骂我了!这个提BUG神器太香了...
前端·javascript·面试