Simple Math Code

mail@pastecode.io avatar
unknown
swift
19 days ago
1.6 kB
7
Indexable
Never
//
//  ContentView.swift
//  Simple Math
//
//  Created by Kiratijuta Bhumichitr on 2/7/2567 BE.
//

import SwiftUI

struct ContentView: View {
    
    @State var number1: String = ""
    @State var number2: String = ""
    @State var result: String = ""
    
    var body: some View {
        VStack {
            
            // TextField 1
            TextField("Enter first number", text: $number1)
                .textFieldStyle(.roundedBorder)
                .keyboardType(.numberPad)
            
            // TextField 2
            TextField("Enter second number", text: $number2)
                .textFieldStyle(.roundedBorder)
                .keyboardType(.numberPad)
            
            // Button
            HStack {
                Button {
                    result = String(Double(number1)! + Double(number2)!)
                } label: {
                    Text("Addition")
                        .padding()
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(8)
                }
                Button {
                    result = String(Double(number1)! * Double(number2)!)
                } label: {
                    Text("Multiply")
                        .padding()
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(8)
                }
            }

            // Text
            Text(result)
        }
        .padding()
    }
}

#Preview {
    ContentView()
}
Leave a Comment