Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
object CounterWithHistory {
var count = 0
var history = List[String]()
def updateUI(): Unit = {
println(s"Counter: $count")
println(s"History: ${history.mkString(", ")}")
}
def increment(): Unit = { count += 1; history = history :+ s"Incremented to $count"; updateUI() }
def decrement(): Unit = { count -= 1; history = history :+ s"Decremented to $count"; updateUI() }
def reset(): Unit = { count = 0; history = List("Counter reset"); updateUI() }
def main(args: Array[String]): Unit = {
updateUI()
increment()
increment()
decrement()
reset()
}
}
CounterWithHistory.main(Array())Coding works best on desktop or with an external keyboard.