Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- data class Vegetable(val name: String, val age: Int, val originCountry: String)
- //Stwórz Listę z trzeba obiektami tej klasy
- val tomato = Vegetable(name = "tomato", age = 10, originCountry = "PL")
- val cabbage = Vegetable(name = "Cabage", age = 200, originCountry = "PL")
- val cucumber = Vegetable(name = "Cucumber", age = 120, originCountry = "PL")
- val x = mutableListOf(tomato, cabbage, cucumber)
- //Stwórz pętlę for w której wypiszesz nazwy tych warzyw
- for (i in x) {
- println(i.name)
- }
- //2. Do zadania powyżej, stwórz sprawdzanie które warzywo jest najstarsze (ma największą wartość age)
- var max: Int = 0
- for (i in x) {
- if (i.age > max) {
- max = i.age
- }
- }
- println(max)
- //3. Stwórz pętle for i wypisz własciwości wszystkich warzy w takiej formie "(name) pochodzi z (originCountry) i ma (age) lat"
- for (i in x) {
- println("${i.name} pochodzi z ${i.originCountry} i ma ${i.age}")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement