generated from admin/template
laba5
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
# Лабораторная работа Kotlin №5
|
||||
|
||||
## 📌 Тема
|
||||
Коллекции в Kotlin (List, MutableList, Set, Map) и функциональные операции над ними в Pokémon TCG
|
||||
|
||||
|
||||
## Ключевой код
|
||||
```csharp
|
||||
fun main() {
|
||||
// --- Ч1
|
||||
val deck: MutableList<String> = mutableListOf()
|
||||
repeat(3) { i ->
|
||||
print("Введите карту №${i + 1}: ")
|
||||
deck.add(readln().trim())
|
||||
}
|
||||
println("Колода собрана (${deck.size} карт): ${deck.joinToString(", ")}")
|
||||
|
||||
print("Какую карту сбросить? ")
|
||||
val wasDiscarded: Boolean = deck.remove(readln().trim())
|
||||
println("✔ Сброшено: $wasDiscarded. Осталось: ${deck.size}")
|
||||
|
||||
// --- Ч2
|
||||
val hand = mapOf(
|
||||
"Pikachu" to "Pokemon",
|
||||
"Potion" to "Trainer",
|
||||
"Fire Energy" to "Energy",
|
||||
"Professor's Research" to "Trainer"
|
||||
)
|
||||
|
||||
val trainerCards = hand.filter { it.value == "Trainer" }
|
||||
val handNames = hand.map { it.key }
|
||||
val energyCount = hand.count { it.value == "Energy" }
|
||||
|
||||
println("Тренеров: ${trainerCards.size} (${trainerCards.keys.joinToString(", ")})")
|
||||
println("Энергии: $energyCount")
|
||||
println("Рука: ${handNames.joinToString(", ")}")
|
||||
|
||||
// --- Ч3
|
||||
print("Сколько энергии прикрепить? ")
|
||||
val energyToAttach = readln().trim().toInt()
|
||||
|
||||
val resources = mutableMapOf("Trainer" to 2, "Energy" to 1)
|
||||
resources["Energy"] = resources.getValue("Energy") + energyToAttach
|
||||
|
||||
for ((type, count) in resources) println("$type: $count шт.")
|
||||
|
||||
println("ИТОГ: Колода: ${deck.size} | Рука: ${hand.size} | Энергия: ${resources.getValue("Energy")}")
|
||||
}
|
||||
Reference in New Issue
Block a user