《软件测试技术》课程第四周随笔

这次博客的内容为,编写一个判断年份是否为闰年的小程序,并进行测试。要求注意输入错误的情况,包括输入的字符不是一个合法的数字等。

1. 问题描述

输入一个年份,判断其是否为一个闰年

2. 实现功能

在文本框中输入数字,点击确认按钮后会弹出信息框,信息框会指出文本框中的年份是否是闰年,如文本框输入内容不合法,也会指出。

3. 测试用例

编号 测试数据 预期结果
1 400的倍数 闰年
2 不是400的倍数但是100的倍数 平年
3 不是100的倍数但是4的倍数 闰年
4 不是4的倍数 平年
5 0 闰年
6 负数 按照前面的规则决定
7 带有前导0和正负号的整数 按照前面的规则决定
8 过大或者过小的整数 指出输入内容不合法
9 小数 指出输入内容不合法
10 带有特殊符号的字符串,非数字 指出输入内容不合法

4. 代码实现

使用C#编写,具体代码如下。

这是C#自动生成的,描述GUI的Form1.Designer.cs的代码:

namespace csharptest
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(24, 51);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(205, 21);
            this.textBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(32, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(155, 24);
            this.label1.TabIndex = 1;
            this.label1.Text = "请输入一个年份~
我们帮您判断其是否为闰年~";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(85, 82);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "确认";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(255, 118);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Is It a leap year?";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
    }
}

这是其他代码,有关于判断一个年份是否为闰年的部分,文件名为Form1.cs:

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 csharptest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool isLeapYear(int x)
        {
            if (x % 400 == 0) return true;
            if (x % 100 == 0) return false;
            if (x % 4 == 0) return true;
            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String ans;
            try
            {
                int x = Convert.ToInt32(textBox1.Text);
                if (isLeapYear(x))
                    ans = "公元" + x + "年是闰年";
                else
                    ans = "公元" + x + "年不是闰年";
                if (x < -1000 || x > 3000)
                    ans += "
提示:您查询的年份过早或过晚,可能您输入有误";
            } catch (Exception exp) {
                ans = "输入数据有误!
" + exp.Message;
            }
            MessageBox.Show(ans);
        }

    }
}

5. 测试结果

编号 输入 输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14

6. 分析与讨论

本程序对于各种输入均能得到较好的结果。但是考虑到闰年应该在某年之后才有定义,所以应该结合历史因素对合法的输入范围做进一步的缩小处理。对于在定义闰年之前的年份均给出平年的结果,并提示该年的人们还不知道什么是闰年...

原文地址:https://www.cnblogs.com/jinzhao1994/p/4395974.html