0

我正在尝试验证客户是否至少 25 岁,以便租车。我相信我有正确的代码来计算我的客户类中的年龄,但它实际上并没有在我的代码中验证它以进行应用

来自 customer.cs 的代码

 public int Age
    {
        get
        {
            // Get today's date
            DateTime currDate = DateTime.Now;

            // Get the difference in years
            int age = currDate.Year - birthdayPicker.Year;

            // Subtract another year if we're before the
            // birth day in the current year
            if (currDate.Month < birthdayPicker.Month || (currDate.Month == birthdayPicker.Month && currDate.Day < birthdayPicker.Day))
                age--;

            return age;
        }
    }

来自 form1.cs 的代码(仍在使用应用按钮,但在继续之前尝试验证年龄)

private void applyButton_Click(object sender, EventArgs e)
    {

        if (myCust.Age >= (25))
        {
            maketextbox.Text = myVehicle.Make;
            modelTextbox.Text = myVehicle.Model;
            yearTextbox.Text = Convert.ToString(myVehicle.Year);
            vinTextbox.Text = Convert.ToString(myVehicle.Vin);
            MessageBox.Show("Congratulations, you have been approved to rent a car!");
        }

        else
        {
            MessageBox.Show("You are not old enough to rent a car.");
        }
4

1 回答 1

0

您的比较代码中可能有错误:

 // Subtract another year if we're before the
 // birth day in the current year
 if (currDate.Month < birthdayPicker.Month || (currDate.Month >= birthdayPicker.Month && currDate.Day < birthdayPicker.Day))
            age--;

注意: == 运算符改为 >=

于 2013-04-28T22:49:49.113 回答