Advertisement
Diaxon

Untitled

Sep 18th, 2023
1,432
0
120 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.04 KB | None | 0 0
  1. fun main() {
  2.     data class Vegetable(val name: String, val age: Int, val originCountry: String)
  3.  
  4.     //Stwórz Listę z trzeba obiektami tej klasy
  5.     val tomato = Vegetable(name = "tomato", age = 10, originCountry = "PL")
  6.     val cabbage = Vegetable(name = "Cabage", age = 200, originCountry = "PL")
  7.     val cucumber = Vegetable(name = "Cucumber", age = 120, originCountry = "PL")
  8.  
  9.     val x = mutableListOf(tomato, cabbage, cucumber)
  10.     //Stwórz pętlę for w której wypiszesz nazwy tych warzyw
  11.  
  12.     for (i in x) {
  13.         println(i.name)
  14.     }
  15.  
  16.     //2. Do zadania powyżej, stwórz sprawdzanie które warzywo jest najstarsze (ma największą wartość age)
  17.  
  18.     var max: Int = 0
  19.  
  20.     for (i in x) {
  21.         if (i.age > max) {
  22.             max = i.age
  23.         }
  24.     }
  25.     println(max)
  26.  
  27.     //3. Stwórz pętle for i wypisz własciwości wszystkich warzy w takiej formie "(name) pochodzi z (originCountry) i ma (age) lat"
  28.  
  29.     for (i in x) {
  30.         println("${i.name} pochodzi z ${i.originCountry} i ma ${i.age}")
  31.  
  32.     }
  33.  
  34.  
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement