C# 四则运算及省市选择及日月选择
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Text = "If语法计算";
button2.Text = "switch语法计算";
label2.Text = "省";
label3.Text = "市";
} private void Button1_Click(object sender, EventArgs e)
{
int text1 = Convert.ToInt32(textBox1.Text);
int text2 = Convert.ToInt32(textBox2.Text);
int text3 = ; //用if的时候需要对变量进行赋值,不然在if语句里会报错
if (comboBox1.Text == "+" && comboBox1.SelectedItem == "+")
//由上一行可以看出combox的text和selecteditem的值一样,
//但是建议使用后者,所以好的写法如下面的
{
text3 = text1 + text2;
}
else if (comboBox1.Text == "-")
{
text3 = text1 - text2;
}
else if (comboBox1.Text == "*")
{
text3 = text1 * text2;
}
else if (comboBox1.Text == "/")
{
if (text2 == )
{
MessageBox.Show("除数不能为0");
return;
}
else
{
text3 = text1 / text2;
}
}
else
{
MessageBox.Show("不知道该怎么计算");
}
textBox3.Text = Convert.ToString(text3);
} private void Button2_Click(object sender, EventArgs e)
{
try
{
int text1 = Convert.ToInt32(textBox1.Text);
int text2 = Convert.ToInt32(textBox2.Text);
int text3;//switch时候就不需要先对变量赋值
switch (comboBox1.SelectedItem)
{
case "+":
text3 = text1 + text2;
break;
case "-":
text3 = text1 - text2;
break;
case "*":
text3 = text1 * text2;
break;
case "/":
//if (text2 == 0)
//{
// MessageBox.Show("除数不能为0");
// return;
//}
text3 = text1 / text2;
break;
default:
MessageBox.Show("不知道该怎么计算");
return;
//throw new Exception("不知道怎么计算");也可以用这个
}
textBox3.Text = Convert.ToString(text3);
}
catch (Exception ex)
{ MessageBox.Show("程序出现意外" + ex.Message);
} }
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedItem == "江苏省")
{
comboBox3.Items.Clear();//每次选择省时,清空市的内容
comboBox3.Items.Add("苏州");
comboBox3.Items.Add("张家港");
comboBox3.Items.Add("昆山");
comboBox3.Items.Add("吴江");
}
if (comboBox2.SelectedItem == "山东省")
{
comboBox3.Items.Clear();
comboBox3.Items.Add("青岛1");
comboBox3.Items.Add("青岛2");
comboBox3.Items.Add("青岛3");
comboBox3.Items.Add("青岛4");
}
if (comboBox2.SelectedItem == "浙江省")
{
comboBox3.Items.Clear();
comboBox3.Items.Add("杭州");
comboBox3.Items.Add("义乌");
comboBox3.Items.Add("温州");
comboBox3.Items.Add("台州");
}
}
private void ComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ } private void Form1_Load(object sender, EventArgs e)
//初始化月份的下拉选择项
{
comboBox4.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox4.Items.Clear();
for (int i = ; i < ; i++)
{
//comboBox4.Items.Add(i.ToString());//这个也是类型转换
comboBox4.Items.Add(Convert.ToString(i));
}
}
private void ComboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
switch (Convert.ToInt32(comboBox4.SelectedItem))
{
case :
case :
case :
case :
case :
case :
case :
comboBox5.Items.Clear();
for (int i = ; i < ; i++)
{
comboBox5.Items.Add(Convert.ToString(i));
}
break;
case :
comboBox5.Items.Clear();
for (int i = ; i < ; i++)
{
comboBox5.Items.Add(Convert.ToString(i));
}
break;
default:
comboBox5.Items.Clear();
for (int i = ; i < ; i++)
{
comboBox5.Items.Add(Convert.ToString(i));
}
break;
}
}
private void Button3_Click(object sender, EventArgs e)
{
string str = string.Format("你选择了{0}月{1}日", comboBox4.SelectedItem, comboBox5.SelectedItem);
MessageBox.Show(str);
string str1 = comboBox4.SelectedItem + "月" + comboBox5.SelectedItem + "日";
MessageBox.Show(str1);
}
}
}
C# 四则运算及省市选择及日月选择的更多相关文章
- Pandas之容易让人混淆的行选择和列选择
在刚学Pandas时,行选择和列选择非常容易混淆,在这里进行一下讨论和归纳 本文的数据来源:https://github.com/fivethirtyeight/data/tree/master/fa ...
- 选择Android还是选择JavaEE?
很多同学咨询过同样的一个问题,该问题也是最备受争议的问题,那就是到底是选择Android还是选择JavaEE.下面发表一些本人的看法. Android属于一个特有的Java技术应用,专注于 ...
- JS-加载页面的时候自动选择刚才所选择option
<body class="no-skin" onload="option_auto(${pd.PACK_SORT})"> <select na ...
- 选择排序—简单选择排序(Simple Selection Sort)
基本思想: 在要排序的一组数中,选出最小(或者最大)的一个数与第1个位置的数交换:然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素 ...
- CSS选择符-----元素选择符
通配选择符(*) 选定所有对象 通配选择符(Universal Selector) 通常不建议使用通配选择符,因为它会遍历并命中文档中所有的元素,出于性能考虑,需酌情使用 & ...
- 选择问题(选择数组中第K小的数)
由排序问题可以引申出选择问题,选择问题就是选择并返回数组中第k小的数,如果把数组全部排好序,在返回第k小的数,也能正确返回,但是这无疑做了很多无用功,由上篇博客中提到的快速排序,稍稍修改下就可以以较小 ...
- 选择排序—简单选择排序(Simple Selection Sort)原理以及Java实现
基本思想: 在要排序的一组数中,选出最小(或者最大)的一个数与第1个位置的数交换:然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素 ...
- jquery操作checkbox方法(全选、全不选、至少选择一个、选择值/文本)
原文:http://blog.csdn.net/u014079773/article/details/52371382 在实际开发中我们经常操作checkbox,不仅仅要获得checkbox选中的值, ...
- project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了
project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了
随机推荐
- Catch That Cow (简单BFS+剪枝)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- c语言秋季作业3
本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 作业链接 我在这个课程的目标是 运用C语言编程解决一些简单的数学问题 这个作业在那个具体方面帮助我实现目标 学习if else ...
- CTF--HTTP服务--命令执行(使用集成工具测试)
开门见山 1. 扫描靶机ip,发现PCS 192.168.1.158 2. 用sparta工具对靶机进行信息探测 3. 扫描结果 4. 在浏览器中查看80端口的页面 5. 查看网站信息 6. 暴力破解 ...
- java基础算法(一):最大子序列和问题的多种算法思路
问题: /** * 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和,并顺序打印子序列. * 示例: * 输入: [-2,1,-3,4,-1,2,1 ...
- logback 发送邮件和自定义发送邮件;java类发送邮件
使用logback发送邮件 需求: 1.报错发邮件,定位错误位置以尽快解决:(报错发送邮件) 2.某一项重要操作完成之后发送邮件:(自定义发送邮件) 没有接触过logback,怎么办? 没办法,硬着头 ...
- IDEA | 识别不出自建webapp文件夹
背景: 今天自建了一个webapp文件夹,发现idea识别不出来是web应用的资源文件夹 解决方案 打开project structure配置,如下图
- Activity--Eclipse安装Activity designer插件失败
案例 今天使用Eclipse 安装Activity designer插件时,出现了如下错误: An error occurred while collecting items to be instal ...
- Maven debug异常:Source not found.
异常 用Maven debug 时出现了Source not found.,在调试过程中尝试添加源码也没有效果 解决方案 先把当前正在运行中的项目停止 右键项目 -> Debug As -> ...
- Kafka动态配置实现原理解析
问题导读 Apache Kafka在全球各个领域各大公司获得广泛使用,得益于它强大的功能和不断完善的生态.其中Kafka动态配置是一个比较高频好用的功能,下面我们就来一探究竟. 动态配置是如何设计的? ...
- Arduino通信篇系列之print()和write()输出方式的差异
我们都知道,在HardwareSerial类中有print()和write()两种输出方式, 两个都可以输出数据,但其输出方式并不相同. 例子: float FLOAT=1.23456; int IN ...