【发布时间】:2017-12-02 09:43:45
【问题描述】:
以下程序会根据客户年龄来验证客户是否有资格观看某部电影。我遇到了 CustomerAgeCheck() 方法的问题。每次我输入年龄高于 100 或低于 0 时,循环都会继续无限运行,并且标签上不会显示任何结果。
protected void okButton_Click(object sender, EventArgs e)
{
AgeVerification();
CostOfTickets();
}
protected int CustomerAgeCheck()
{
int age = int.Parse(Cust1AgeTextBox.Text);
do
{
ageVerificationLabel.Text = String.Format("Please enter the correct age");
} while (age < 0 || age > 100);
return age;
}
protected void AgeVerification()
{
int age = CustomerAgeCheck();
if (Movie3RadioButton.Checked && age < 17)
{
ageVerificationLabel.Text = String.Format("Access denied - you are too young");
}
else if (Movie4RadioButton.Checked || Movie5RadioButton.Checked || Movies6RadioButton.Checked && age < 13)
{
ageVerificationLabel.Text = String.Format("Access deniad - you are too young");
}
else
{
ageVerificationLabel.Text = String.Format("Enjoy your Movie");
}
}
protected void CostOfTickets()
{
int cost;
int totalTickets = int.Parse(CustomerDropDownList.SelectedValue);
cost = totalTickets * 10;
resultLabel.Text = String.Format("Your Total is {0:C}", cost);
}
【问题讨论】:
-
n 为什么要使用循环呢?在
AgeVerification中,您有类似的逻辑来进行输入验证。你为什么在CustomerAgeCheck中切换到别的东西?请记住,如果这是 asp.net(基于您如何标记它),Cust1AgeTextBox.Text中的值只会在用户再次发布表单后发生变化。您不能在循环中等待以等待新值的进入。这不是网络的工作方式。 -
你永远不会改变
age的值,所以条件总是为真。