我正在尝试验证sVenueDay(通过文本框输入的文本),以确保输入的值是有效的一天。我进入"星期天"进入txtBoxVenueDay.Text。运行程序时,"输入的输入无效日期"即使"星期日"输入是有效的一天。我尝试使用!sVenueDay.Equals(" Sunday")格式(以及其他日子)但没有:/
string sVenueDay = txtBoxVenueDay.Text;
if (sVenueDay != "Monday" || sVenueDay != "Tuesday" || sVenueDay != "Wednesday" || sVenueDay != "Thursday" || sVenueDay != "Friday" || sVenueDay != "Saturday" || sVenueDay != "Sunday")
{
lblOutput.Text = "Input entered not valid day";
return;
}
else
lblOutput.Text = "Valid day";
答案 0 :(得分:0)
您正在检查它是否不等于“星期一”或它不等于“星期二”。你能提出哪个字符串等于“星期一”和“星期二”? :)
我怀疑你想要:
if (sVenueDay != "Monday" && sVenueDay != "Tuesday" && ...)
或者更有用的是:
private static readonly HashSet<string> ValidDays = new HashSet<string>(
new[] { "Monday", "Tuesday", ... });
...
if (!ValidDays.Contains(sVenueDay))
{
...
}
答案 1 :(得分:0)
当你的使用或第一个条件为真时,我不会检查下一个条件。所以先检查true然后为false;尝试如下:(使用equals方法) string sVenueDay = txtBoxVenueDay.Text; if(sVenueDay ==“Monday”|| sVenueDay ==“Tuesday”|| sVenueDay ==“Wednesday”|| sVenueDay ==“Thursday”|| sVenueDay ==“Friday”|| sVenueDay ==“星期六”|| sVenueDay ==“星期日”){lblOutput.Text =“有效日”;返回; } else lblOutput.Text =“输入的输入不是有效日期”;