網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡單的計(jì)算器小功能的具體代碼,供大家參考,具體內(nèi)容如下
先來張效果圖吧(5分鐘寫好,莫怪)
代碼:
數(shù)字按鈕綁定的是button_Clickd()方法
運(yùn)算符按鈕綁的是Button_Clickp()方法
思想:按下數(shù)字按鈕,將數(shù)字按鈕的值連接到textbox上,然后按下運(yùn)算符判斷是否為等于,并記錄運(yùn)算符的內(nèi)容,以便后面做處理。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; ? namespace WindowsFormsApplication4 { ? ? public partial class Form2 : Form ? ? { ? ? ? ? private string s; ? ? ? ? private double x, y; ? ? ? ? private Button btn; ? ? ? ? ? ? ? ? public Form2() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? ? ? private void Form2_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? label1.Text=""; ? ? ? ? ? } ? ? ? ? private void buttond_Click(object sender, EventArgs e)//數(shù)字符所綁定的事件 ? ? ? ? { ? ? ? ? ? ? btn = (Button)sender; ? ? ? ? ? ? textBox1.Text = textBox1.Text + btn.Text;//將所點(diǎn)擊的數(shù)字付呈現(xiàn)在textBox上面 ? ? ? ? ? } ? ? ? ? private void buttonp_Click(object sender, EventArgs e)//運(yùn)算符所綁定的事件 ? ? ? ? { ? ? ? ? ? ? btn = (Button)sender; ? ? ? ? ? ? if (btn.Name != "button12")//如果不是"=" ? ? ? ? ? ? { ? ? ? ? ? ? ? ? x = Convert.ToDouble(textBox1.Text);//將所所輸入的第一個(gè)字符保留下來 ? ? ? ? ? ? ? ? textBox1.Text = "";//清空textBox的內(nèi)容 ? ? ? ? ? ? ? ? s = btn.Name;//獲取運(yùn)算符的種類 ? ? ? ? ? ? ? ? label1.Text = x.ToString();//將第一個(gè)所按的字符輸出來 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (label1.Text == "") ? ? ? ? ? ? ? ? ? ? MessageBox.Show("輸入不正確!!", "信息提示", MessageBoxButtons.OK); ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? y = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? ? ? ? ? switch (s)//使用s來判讀所按的按鈕 ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? case "button13": ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x + y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case "button14": ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x - y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case "button15": ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x * y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? case "button16": ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (y == 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("除零錯(cuò)誤!!!", "信息提示", MessageBoxButtons.OK); ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x / y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? label1.Text = textBox1.Text; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? } }
原文鏈接:https://blog.csdn.net/qq_38345598/article/details/79561013
相關(guān)推薦
- 2022-04-14 Mac快速解決zsh: command not found: conda的方法
- 2022-06-07 如何在Python中妥善使用進(jìn)度條詳解_python
- 2022-08-10 pandas溫差查詢案例的實(shí)現(xiàn)_python
- 2022-09-01 C#實(shí)現(xiàn)簡單工廠模式_C#教程
- 2023-03-20 Redis使用Bitmap的方法實(shí)現(xiàn)_Redis
- 2022-07-24 python單向循環(huán)鏈表實(shí)例詳解_python
- 2022-05-25 python教程之利用pyautogui圖形自動(dòng)化擊敗重復(fù)性辦公任務(wù)_python
- 2022-06-22 android實(shí)現(xiàn)注冊登錄程序_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支