循环数组时间C#Winforms
假设我有一个密码数组 以及当前时间的验证。例如,您的日程安排为上午7:30:00到11:30:00,如果尚未到7:30,并且已经超过11:30,则无法获取密码。但在这段时间内,你可以得到你的密码。一旦点击按钮1(w/c表示您已经被授权-正确的时间),就会出现一个消息框,显示如下内容: 你的密码是111111循环数组时间C#Winforms,c#,arrays,winforms,loops,time,C#,Arrays,Winforms,Loops,Time,假设我有一个密码数组 以及当前时间的验证。例如,您的日程安排为上午7:30:00到11:30:00,如果尚未到7:30,并且已经超过11:30,则无法获取密码。但在这段时间内,你可以得到你的密码。一旦点击按钮1(w/c表示您已经被授权-正确的时间),就会出现一个消息框,显示如下内容: 你的密码是111111 以此类推,直到所有的密码都用完了,然后它会再次回到密码111111、2222222、333333,等等。。。我该怎么做?比如什么时间格式?关于时间比较,我感到很难过。谢谢。该功能仅在一天中的
以此类推,直到所有的密码都用完了,然后它会再次回到密码111111、2222222、333333,等等。。。我该怎么做?比如什么时间格式?关于时间比较,我感到很难过。谢谢。该功能仅在一天中的几个小时内检查时间 您可以使用如下所示的模块化算法来包装密码 以下是我的作品:
private int currentPassword = -1;
private int[] passwords = new int[]{111111,222222,333333,444444,555555};
private DateTime startTime = new DateTime(2012, 7, 18, 22, 0, 0);
private DateTime endTime = new DateTime(2012, 7, 18, 22, 15, 0);
private void button1_Click(object sender, EventArgs e)
{
if (isValidTime(DateTime.Now))
{
currentPassword++;
currentPassword = currentPassword % passwords.Length;
MessageBox.Show(passwords[currentPassword].ToString());
}
else
{
MessageBox.Show( "Try again at a different time" );
}
}
private bool isValidTime( DateTime now )
{
if ( startTime.TimeOfDay.CompareTo(now.TimeOfDay) <= 0)
{
if ( now.TimeOfDay.CompareTo(endTime.TimeOfDay) <= 0)
{
return true;
}
}
return false;
}
private int currentPassword=-1;
私有int[]密码=新int[]{111111 22222 333333 444444 555555};
private DateTime startTime=新日期时间(2012,7,18,22,0,0);
private DateTime endTime=新的日期时间(2012,7,18,22,15,0);
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(isValidTime(DateTime.Now))
{
当前密码++;
currentPassword=currentPassword%passwords.Length;
Show(密码[currentPassword].ToString());
}
其他的
{
Show(“在其他时间重试”);
}
}
private bool isValidTime(日期时间现在)
{
如果(startTime.TimeOfDay.CompareTo)(now.TimeOfDay)您正在索引到4元素数组中的第五个元素供参考。您的代码示例将崩溃。它工作正常,但当我关闭消息框并尝试获取下一个密码(即2222)时,仍然是111111。为什么?@CantHandleMeBabe您是否碰巧关闭了程序,而不是仅仅关闭了消息框?因为,每次显示msgBox之前,我们都会增加密码。如果您希望当前密码值在应用程序运行之间保持不变,请在问题中提及。
private int currentPassword = -1;
private int[] passwords = new int[]{111111,222222,333333,444444,555555};
private DateTime startTime = new DateTime(2012, 7, 18, 22, 0, 0);
private DateTime endTime = new DateTime(2012, 7, 18, 22, 15, 0);
private void button1_Click(object sender, EventArgs e)
{
if (isValidTime(DateTime.Now))
{
currentPassword++;
currentPassword = currentPassword % passwords.Length;
MessageBox.Show(passwords[currentPassword].ToString());
}
else
{
MessageBox.Show( "Try again at a different time" );
}
}
private bool isValidTime( DateTime now )
{
if ( startTime.TimeOfDay.CompareTo(now.TimeOfDay) <= 0)
{
if ( now.TimeOfDay.CompareTo(endTime.TimeOfDay) <= 0)
{
return true;
}
}
return false;
}