Advertisement
QwertyAvatar

Swift 1

Oct 3rd, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.40 KB | None | 0 0
  1. //
  2. //  main.swift
  3. //  Zad1.1
  4.  
  5.  
  6. import Foundation
  7.  
  8. var suma, liczba: Int
  9. var srednia: Double
  10. suma=0
  11.  
  12. for _ in 0..<3 {
  13.     liczba = Int.random(in:1..<101)
  14.     suma+=liczba
  15.     print("Liczba to:", liczba)
  16. }
  17. print("Suma liczb to: ", suma)
  18. srednia = (Double)(suma)/3.00
  19. print("Srednia liczb to: ", srednia)
  20.  
  21. ////////////////////////////////////////////////
  22.  
  23. //
  24. //  ContentView.swift
  25. //  Zad1.2
  26. // ?????
  27.  
  28. import SwiftUI
  29.  
  30. struct ContentView: View {
  31.     @State private var a1: String = ""
  32.     @State private var a2: String = ""
  33.     @State private var a3: String = ""
  34.     @State var suma: Int = 0
  35.     @State var srednia: Double = 0.0
  36.     @State var pomoc: Bool = false
  37.     var body: some View {
  38.         VStack{
  39.             Text("Podaj 3 liczby:")
  40.                 .padding()
  41.             TextField("Podaj 1: ", text: $a1)
  42.             TextField("Podaj 2: ", text: $a2)
  43.             TextField("Podaj 3: ", text: $a3)
  44.            
  45.             Button(action:{
  46.                 pomoc = true
  47.                 self.suma = (Int)(a1)! + (Int)(a2)! + (Int)(a3)!
  48.                 self.srednia = (Double)(suma/3)
  49.                
  50.             }, label: {
  51.                 Text("Oblicz")})
  52.            
  53.             if(pomoc){Text("Suma liczb: \(suma), a srednia: \(srednia)")}
  54.     }
  55. }
  56.  
  57. struct ContentView_Previews: PreviewProvider {
  58.     static var previews: some View {
  59.         ContentView()
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement