习题:

习题.jpg
写在前面:关于窗体程序的设计,和dos界面全部复制代码就能用,是完全不一样的。最重要的是在图形界面里面的命名要清楚,这样在代码界面编写时,就不会不能一一对应,就算有了别人的代码,不能和他的图形界面命名一样,也一定会报错。比如在第一题中,如果你的输入名字的文本框名字不是textName,那你用我的代码肯定就报错了。
练习1
这题比较简单,用if语句判断字符串是否相等就好了。
以下是界面设计:
界面.PNG
以下是对应的代码文件
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 practise1
{
public partial class FormInfo : Form
{
public FormInfo()
{
InitializeComponent();
}
private void FormInfo_Load(object sender, EventArgs e)
{
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string Name,Sex, Id;
Name = textName.Text;
Sex = textSex.Text;
Id = textId.Text;
if (Name != "李药师")
MessageBox.Show("姓名输入错误", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
//以下仿照上面
else if (Sex != "男")
MessageBox.Show("性别输入错误", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
else if (Id != "17721345")
MessageBox.Show("学号输入错误", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
else
MessageBox.Show("输入正确!");
}
}
}
运行结果:

结果1.png

结果2.png
练习2
第二个设计计算器稍微难了那么一丢丢,我在网上参考了一下高人的代码,最后也搞出来了。一个是怎么让所有的数字按键都能对一个变量起作用,把按下的第一个数值保存下来,而第二个数值可直接由内置的方法.text得到,再用一个switch选择,然后算出结果放到文本框中。
最重要的一点就是,按下一个键之前要先把之前在文本框中的内容清除掉,这样,才能保证由内置的方法.text得到的是第二个数值。
还有就是需要考虑除法除数不为0以及重置按钮用于多次计算的问题。
以下设计界面
界面.PNG
以下是代码设计
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 practise2
{
public partial class MyCalculator : Form
{
//定义全局变量,大家都可以访问,我一开始就是不知道怎么让所有的方法能
//都对一个变量起作用
double result = 0;//存储结果
double temp = 0;//存储第一个数,第二个数直接通过方法获得
bool c = false;//判断有没有运算符
string d;//运算符
public MyCalculator()
{
InitializeComponent();
}
//按下等号=键
private void Result_Click(object sender, EventArgs e)
{
switch(d)
{
case "+":
result = temp + Convert.ToInt32(textShow.Text);
break;
case "-":
result = temp - Convert.ToInt32(textShow.Text);
break;
case "×":
result = temp * Convert.ToInt32(textShow.Text);
break;
case "÷":
result = temp / Convert.ToInt32(textShow.Text);
break;
}
textShow.Text = Convert.ToString(result);
}
//按不同数字按钮
private void btn1_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "1";
}
//以下仿照
private void btn2_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清除运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "2";
}
private void btn3_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "3";
}
private void btn4_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "4";
}
private void btn5_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "5";
}
private void btn6_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "6";
}
private void btn7_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "7";
}
private void btn8_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "8";
}
private void btn9_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
}
//显示它
textShow.Text += "9";
}
private void btn0_Click(object sender, EventArgs e)
{
if (c == true)//说明已经有了运算符,这是第二个数
{
textShow.Text = "";//清空运算符,为显示第二个数作准备
c = false;//重置c
if (d == "÷")
{
//textShow.Clear();
MessageBox.Show("除数不能为0!", "错误提示", MessageBoxButtons.OK,
MessageBoxIcon.Error);//显示错误信息
}
else
textShow.Text = "0";
}
else
textShow.Text = "0";
}
//按下运算符的响应
private void btnPlus_Click(object sender, EventArgs e)
{
temp = Convert.ToInt32(textShow.Text);
textShow.Text = "+";
d = "+";
c = true;
}
private void btnMinus_Click(object sender, EventArgs e)
{
temp = Convert.ToInt32(textShow.Text);
textShow.Text = "-";
d = "-";
c = true;
}
private void btnMultiply_Click(object sender, EventArgs e)
{
temp = Convert.ToInt32(textShow.Text);
textShow.Text = "×";
d= "×";
c = true;
}
private void btnDivide_Click(object sender, EventArgs e)
{
temp = Convert.ToInt32(textShow.Text);
textShow.Text = "÷";
d = "÷";
c = true;
}
//添加一个归0按钮,用于多次计算
private void btnReset_Click(object sender, EventArgs e)
{
textShow.Text = "";
}
}
}
练习3
这道题很简单,就设置一下窗体的属性,把外观里面的FormBorderStyle选择为FixedToolWindow,把Text重写为我的第一个窗体;把布局里面的StartPosition选择为CenterScreen。就大功告成了!
运行结果
运行结果.PNG