網站首頁 編程語言 正文
本文實例為大家分享了swift實現簡易計算器的具體代碼,供大家參考,具體內容如下
首先在storyboard中搭建出界面
接著上viewcontroller代碼
import UIKit class ViewController: UIViewController { ? ? @IBOutlet weak var result: UILabel! ? ? var n1: String = "" ? ? var n2: String = "" ? ? var n3: String = "" ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ? ? // Do any additional setup after loading the view, typically from a nib. ? ? } ? ? override func didReceiveMemoryWarning() { ? ? ? ? super.didReceiveMemoryWarning() ? ? ? ? // Dispose of any resources that can be recreated. ? ? } ? ? @IBAction func didClick(sender: UIButton) { ? ? ? ? let temp = sender.currentTitle ? ? ? ? if temp == "AC" { ? ? ? ? ? ? n1 = "" ? ? ? ? ? ? n2 = "" ? ? ? ? ? result.text = "0" ? ? ? ? ? ? return ? ? ? ? } ? ? ? ? else if temp == "+" || temp == "-" || temp == "*" || temp == "/" { ? ? ? ? ? n1 = result.text! ? ? ? ? ? n2 = "" ? ? ? ? ? n3 = temp! ? ? ? ? ? return ? ? ? ? } ? ? ? ? else if temp == "=" { ? ? ? ? ? ? var result1: Double = 0 ? ? ? ? ? ? ? ?println("n1:"+n1+" n2:"+n2) ? ? ? ? ? ? switch n3 { ? ? ? ? ? ? ?case "+": ? ? ? ? ? ? ? ? result1 = (n1 as NSString).doubleValue + (n2 as NSString).doubleValue ? ? ? ? ? ? ? ? ? ? ? ?case "-": ? ? ? ? ? ? ? ? result1 = (n1 as NSString).doubleValue - (n2 as NSString).doubleValue ? ? ? ? ? ? ? ?case "*": ? ? ? ? ? ? ? ? result1 = (n1 as NSString).doubleValue * (n2 as NSString).doubleValue ? ? ? ? ? ? ?case "/": ? ? ? ? ? ? ? ? result1 = (n1 as NSString).doubleValue / (n2 as NSString).doubleValue ? ? ? ? ? ? ?default: ? ? ? ? ? ? ? ? result1 = 0 ? ? ? ? ? ? } ? ? ? ? ? ? result.text = "\(result1)" ? ? ? ? ? ? n3 = "" ? ? ? ? ? ? return? ? ? ? ? ?? ? ? ? ? } ? ? ? ?? ? ? ? ? if n3 != "" { ? ? ? ? ? n2 = n2 + temp! ? ? ? ? ? result.text = n2 ? ? ? ? }else { ? ? ? ? ? n1 = n1 + temp! ? ? ? ? ? result.text = n1 ? ? ? ? } ? ? } }
最后效果圖
原文鏈接:https://blog.csdn.net/woqq786325209/article/details/41687163
相關推薦
- 2022-06-08 Spring Boot Start之mqtt框架封裝
- 2022-05-26 openwrt安裝docker并啟動的操作方法_docker
- 2022-01-29 寶塔Linux面板的ftp無法使用連接解決方案
- 2023-04-02 pytorch中關于distributedsampler函數的使用_python
- 2022-04-14 C語言的分支和循環語句你了解嗎_C 語言
- 2022-12-09 Python字符串本身作為bytes進行解碼的問題_python
- 2022-11-01 JetPack?Compose底部導航欄的實現方法詳解_Android
- 2022-04-27 為ABP框架配置數據庫_基礎應用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支