如何使用for循环 [英] How to use the for loop

查看:91
本文介绍了如何使用for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是C#中的初学者用户。我可以在下面的代码中使用for循环吗?我怎么能这样做?

我想一周7天计算。



我尝试了什么:



Hi just beginner user in C#. Can i use for loops at below code? How can i do that?
I want to calculate for 7 days a week.

What I have tried:

DateTime dt_startDay1 = Worktime_dtpicker_startDay1.Value;
DateTime dt_endDay1 = Worktime_dtpicker_endDay1.Value;
int dt_breakDay1 = Int32.Parse(Worktime_numUpDown_breakDay1.Value.ToString());

int diff_Day1 = (int)(dt_endDay1 - dt_startDay1).TotalMinutes;
int total_wtDay1 = diff_Day1 - dt_breakDay1;
label1.Text = Convert.ToString(total_wtDay1);

DateTime dt_startDay2 = Worktime_dtpicker_startDay2.Value;
DateTime dt_endDay2 = Worktime_dtpicker_endDay2.Value;
int dt_breakDay2 = Int32.Parse(Worktime_numUpDown_breakDay2.Value.ToString());

int diff_Day2 = (int)(dt_endDay2 - dt_startDay2).TotalMinutes;
int total_wtDay2 = diff_Day2 - dt_breakDay2;
label2.Text = Convert.ToString(total_wtDay2);

推荐答案

不是真的 - 循环擅长做同样的东西一遍又一遍,但它需要与 迭代 的东西一起工作;每次都会上升或下降相同数量的东西。

你的代码不会这样做 - 只是访问名称变化的变量,这是非常不同的。

相反,我编写了一个接受两个DateTime值和一个整数的方法,并返回一个字符串结果的整数。这样,你不重复相同的代码,只传递不同的值,并将结果存储在正确的位置。
Not really - a loop is good at doing "the same stuff over and over", but it needs to work with something that iterates; something that goes up or down by the same amount each time round.
And your code doesn't do that it - just accesses variables with names that change, which is very different.
Instead, I'd write a method which accepted two DateTime values and an integer, and returned an integer of string result. That way, you don't repeat the same code, just pass the different values, and store the result in the right place.


你可以,但(没有冒犯)更多一点比我觉得你习惯的复杂。无论如何,这样做无环无聊的方式会更快。



然而,如果你想尝试它,你必须存储列表中的Worktime_dtpicker_startDay1,Worktime_dtpicker_startDay2,...,WorkTime_dtpicker_startDay7:
You can, but (no offense) that's a bit more complicated than I feel you're used to. And doing it the no-loop-boring-type-it-all way would be faster anyway.

However, if you want to try it, you have to store Worktime_dtpicker_startDay1, Worktime_dtpicker_startDay2, ..., WorkTime_dtpicker_startDay7 in a list:
List<datetimepicker> startPickers = new List<datetimepicker>()
{
    Worktime_dtpicker_startDay1, Worktime_dtpicker_startDay2,
    Worktime_dtpicker_startDay3, Worktime_dtpicker_startDay4,
    Worktime_dtpicker_startDay5, Worktime_dtpicker_startDay6,
    Worktime_dtpicker_startDay7
}

结束时间选择器相同。 break UpDowns 再次相同。对于结果标签

然后,您可以

Same for the end time pickers. And again the same for the break UpDowns. And for the result labels.
Then, you can

for(int i = 0; i < 7; i++)
{
    int minutes = (int)((endPickers[i] - startPickers[i]).TotalMinutes);
    minutes -= breakUpDowns[i].Value;
    resultLabels[i].Value = minuts.ToString();
}



------------------------------ ---------------------------------

等级2:

完全不是你问的问题,而是阅读 UserControl 并创建一个包含选择器和标签的。然后使用其中的7个而不是所有单独的控件。在每次输入更改时计算usercontrol中的结果。根本不需要循环,但提供响应非常快的UI。


---------------------------------------------------------------
Level 2:
Totally not what you were asking, but read up on UserControls and create one that includes pickers and label. Then use 7 of them instead of all the individual controls. Calculate result within the usercontrol on every input change. No need for a loop at all, but gives a very responsive UI.


这篇关于如何使用for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆