C# 条件每 N 周或每 N 个月做某事

发布于 2025-01-11 17:43:52 字数 2278 浏览 0 评论 0原文

我正在寻找一种有效的方法来每 N 次时间段执行一个过程 (Process1),例如每 2 周或每 6 个月,是用户定义的整数值 (N) 谁可以从键盘输入任何整数值。

目前,当这个过程需要每 1 周或每 1 个月完成一次时,我有代码执行“Process1”,所以到目前为止它工作得很好。

对于 N 个时间段部分,我正在尝试使用基于这些 if 语句和预定义循环内的计数器的下一个逻辑,该循环已经正常工作且无法修改,因此我唯一的部分need to get 是循环内的内容之一。

…
private int  N;          //Integer number defined by the user
private int  i = 1;      //Counter
…

// loop
{
…
     If ( N == 1 )       
     {
                         /*
                            N = 1 would be the default value with which: 1 week (every 1 week), 
                            or 1 month (every 1 month),…
                         */
     
        …
        //do Process1    /*
                            This part is already working as should because is not even needed to specify
                            there is a `N == 1` to have Process1 to be done every week or every month, but
                            I put it as logic guide of how I think it would be the process flow. 
                         */
     }
    
     else if ( N > 1  &&  i < N )
     {
                i++;     //Increment the counter
     }

    If ( i == N )
    {
        …
        //do Process1
        i = 1;
    }
…
}
…

我可以看到这种逻辑的效率问题是,如果 N = 40(例如 40 周),或者由于输入错误而导致更大的数字,那么“i++”进程的代码将在达到执行 Process1 的点之前完成 40 次,这就是为什么我想知道是否有更有效或更简化的方法来指定类似的内容,例如:

…
//To specify Process1 to be done every N weeks, like every 2 weeks, or any other N int number.
If ( ( Period.DayOfWeek == DayOfWeek.Friday) *N )
{
…
    //do Process1
}
…
//To specify Process1 to be done every N months, like every 6 months, or any other N int number.
If ( ( Period == Time.Month) *N )
{
…
    //do Process1
}

一些说明

请注意这是需要与历史数据,无需更改 Process1 的当前完成方式。

另外,由于要求,一般库或外部程序无法完成此操作,也无法通过其他方式过滤历史数据。

可以使用的只是基本代码,例如 if-statementsDateTime/DayOfWeekcounters 以及这些“基本代码”等等因为这是一个非常简单的情况,不需要任何特殊的“功能”。请认为一切都在 100% 完美的状态下进行。 唯一可以修改的部分是 DateTime/DayOfWeek 部分,以便能够将当前方式:“每周/每月做一些事情”更改为所需的方式:< em>“每 N 周/月做某事”。仅此而已。感谢您尝试提供替代方案的每条评论,但这是一个非常基本的情况,只有 DateTime 部分可以修改。

预先感谢您的帮助!

I'm looking for an efficient way to do a process (Process1) every N times of time period, as for example every 2 weeks or every 6 months, being the integer value (N) defined by the user who can enter any integer value from the keyboard.

For now, I have the code doing the ‘Process1’ as should when this process needs to be done every 1 week or every 1 month, so it is working perfectly until this point.

For the N time periods part, I’m trying with something based on the next logic with these if-statements and a counter that are inside a predefined loop that is already working as should and can't be modified, so the only part I need to get is the one of what would be inside the loop.

…
private int  N;          //Integer number defined by the user
private int  i = 1;      //Counter
…

// loop
{
…
     If ( N == 1 )       
     {
                         /*
                            N = 1 would be the default value with which: 1 week (every 1 week), 
                            or 1 month (every 1 month),…
                         */
     
        …
        //do Process1    /*
                            This part is already working as should because is not even needed to specify
                            there is a `N == 1` to have Process1 to be done every week or every month, but
                            I put it as logic guide of how I think it would be the process flow. 
                         */
     }
    
     else if ( N > 1  &&  i < N )
     {
                i++;     //Increment the counter
     }

    If ( i == N )
    {
        …
        //do Process1
        i = 1;
    }
…
}
…

The efficiency issue I can see with this kind of logic is that if N = 40 (40 weeks for example), or a number even larger due a typing error, then the code for ‘i++’ process will be done 40 times before to reach the point to do Process1, and that’s the reason why I was wondering if there is a more efficient or simplified way to maybe just specify something similar like:

…
//To specify Process1 to be done every N weeks, like every 2 weeks, or any other N int number.
If ( ( Period.DayOfWeek == DayOfWeek.Friday) *N )
{
…
    //do Process1
}
…
//To specify Process1 to be done every N months, like every 6 months, or any other N int number.
If ( ( Period == Time.Month) *N )
{
…
    //do Process1
}

Some clarifications:

Please note this is needed to work with historical data and there is no need to change the current way with which Process1 is done.

Also, because the requirements, it can’t be done with libraries in general or with external programs, neither in a way to filter the historical data with some other ways.

What can be used is only basic code like if-statements, DateTime/DayOfWeek, counters and these kind of “basic code”, among other things because it is a very simple situation that doesn’t need any special “feature”. Please think that everything is working as should 100% perfect. And the only part that can be modified is the DateTime/DayOfWeek part in order to be able to just change the current way: “do somthing every week/month” to the required way: “do somthing every N weeks/months”. Only that. Thank you for every comment trying to provide alternatives, but it's a very basic situation and only the DateTime part can be modified.

Thank you in advance for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文