【C#】实验5

s5-1

设计项目s5-1。 设计一个加法速度测试程序,程序启动后显示出如图所示的运行界面。用户单击“开始”按钮后开始计时,并分别在文本框1、2中随机生成并显示加数和被加数,用户在文本框3中输入答案,按回车键后程序在答案的后面指示对错,并显示出下一道题。一分钟以后程序统计出出题数量和作对的数量。用户单击“结束”按钮退出程序。

窗体设计:
在这里插入图片描述

MainForm.cs:

/*
 * 作者:JeronZhou
 * 日期: 2021-10-11
 * 功能:加法速度测试程序
 */

using System;
using System.Linq;
using System.Windows.Forms;

namespace s5_1
{
	public partial class MainForm : Form
	{
		int add1,add2,sum;
		int Correct=0,Count=0;
		Random rand = new Random();  //随机数
		public MainForm()
		{
			InitializeComponent();
		}
		void Button1Click(object sender, EventArgs e)  //开始按钮
		{
            button1.Enabled=false;
            button2.Enabled=false;
			timer.Enabled=true;
			timer.Start();  //计时开始
			add1 = rand.Next(0, 101);
            add2 = rand.Next(0, 101);
            textBox1.Text = add1.ToString();
            textBox2.Text = add2.ToString();
            textBox3.Focus();  //焦点
		}
		void Button2Click(object sender, EventArgs e)  //结束按钮
		{
			Application.Exit();
		}
		void TimerTick(object sender, EventArgs e)  //计时器结束
		{
			timer.Stop();
			timer.Enabled=false;
			MessageBox.Show(string.Format("题目总数为{0},做对题数为{1}.", Count, Correct), "答题结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
			Count=0;
			Correct=0;
			button1.Enabled=true;
			button2.Enabled=true;
		}
		public void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
			if(e.KeyChar==13 && timer.Enabled==true)  //回车
            {
                Count++;
                sum = add1 + add2;
                if (textBox3.Text.ToString() == sum.ToString())
                {
                    Correct++;
                }
                add1 = rand.Next(0, 101);
                add2 = rand.Next(0, 101);
                textBox1.Text = add1.ToString();
                textBox2.Text = add2.ToString();
                textBox3.Text = "";
            }
        }
	}
}

Program.cs:

/*
 * 作者:JeronZhou
 * 日期: 2021-10-11
 * 功能:加法速度测试程序
 */

using System;
using System.Windows.Forms;

namespace s5_1
{
	internal sealed class Program
	{
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
	}
}

测试结果:
运行程序,显示如下界面:
在这里插入图片描述
点击开始按钮,开始1分钟计时,效果如下:
在这里插入图片描述
人为控制做对2题,做错一题,1分钟后停止计时,显示结果正确:
在这里插入图片描述

s5-2

设计项目s5-2。窗体上有一张图片,默认是隐藏的,用户在文本框中输入身份证号(131226198105223452),点击按钮,如果年龄大于18岁则显示图片,否则提示年龄太小。

MainForm.cs:

/*
 * 作者:JeronZhou
 * 日期: 2021-10-11
 * 功能:大于18岁显示图片
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace s5_2
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		bool Check(int y,int m,int d)  //判断是否满18岁
		{
			bool flag;
            int Year = int.Parse(DateTime.Now.Year.ToString());
            int Month = int.Parse(DateTime.Now.Month.ToString());
            int Day = int.Parse(DateTime.Now.Day.ToString());
            if(Year-y>18)
            {
            	flag=true;
            }
            else if(Year-y==18)
            {
            	if(Month>m)
            	{
            		flag=true;
            	}
            	else if(Month==m)
            	{
            		if(Day<=d)
            		{
            			flag=true;
            		}
            		else
            		{
            			flag=false;
            		}
            	}
            	else
            	{
            		flag=false;
            	}
            }
            else
            {
            	flag=false;
            }
            return flag;
		}
		void ButtonClick(object sender, EventArgs e)  //确认按钮
		{
			string number = textBox.Text.ToString();
			int year = int.Parse(number.Substring(6,4));
			int month = int.Parse(number.Substring(10,2));
			int day = int.Parse(number.Substring(12,2));
			if(Check(year,month,day)==true)  //大于18岁则显示图片
			{
				pictureBox.Visible=true;
			}
			else
			{
				MessageBox.Show("您还未满18岁,无法查看图片!");
			}
		}
	}
}

Program.cs:

/*
 * 作者:JeronZhou
 * 日期: 2021-10-11
 * 功能:大于18岁显示图片
 */

using System;
using System.Windows.Forms;

namespace s5_2
{
	internal sealed class Program
	{
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
	}
}

测试结果:
运行程序,显示如下界面,此时不显示图片:
在这里插入图片描述
输入身份证号码样例“131226198105223452”,点击确认按钮,即可显示图片。

输入身份证号码样例“131226201005223452”,点击确认后无法显示图片,并弹出提示语,效果如下:

在这里插入图片描述

解决方案源代码

C#实验5 解决方案及项目源代码压缩包

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeron Zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值