【发布时间】:2021-08-27 06:25:57
【问题描述】:
我正在构建一个 c# 控制台应用程序。
假设用户输入为
- 以小时为单位的任务工作量 = 48
- 每天工作小时数 = 9
- 开始日期 = 26/02/2021
排除周末和公共假期后,如何显示任务的结束日期。
我已经通过考虑所有可能的验证来实现用户输入。
static void Main(string[] args)
{
//effort hours
Console.Write("Enter No of hours: ");
int hours;
if (Int32.TryParse(Console.ReadLine(), out hours))
{
if (hours <= 0)
{
Console.WriteLine("Hours of effort cannot be a negative value or 0");
}
else
{
Console.WriteLine("The no of hours entered is: " + hours);
}
}
else
{
Console.WriteLine("You have entered an incorrect value.");
}
Console.ReadLine();
//working hours per day
Console.Write("Enter No of working hours per day: ");
int WorkingHours;
if (Int32.TryParse(Console.ReadLine(), out WorkingHours))
{
if (WorkingHours <= 0 || WorkingHours > 9)
{
Console.WriteLine("Maximum working hours per day is 9 hours and no of hours entered cannot be 0 or negative");
}
else
{
Console.WriteLine("The no of working hours entered is: " + WorkingHours);
}
}
else
{
Console.WriteLine("You have entered an incorrect value.");
}
Console.ReadLine();
//Enter start date
Console.WriteLine("Enter the start date date (e.g. dd/mm/yyyy): ");
DateTime startDate;
{
while (!DateTime.TryParse(Console.ReadLine(), out startDate))
{
Console.WriteLine("You have entered an incorrect value.");
Console.WriteLine("Enter the start date date (e.g. dd/mm/yyyy): ");
}
Console.WriteLine("The startdate is: " + startDate);
}
}
【问题讨论】:
-
你能定义公共假期吗?不同的国家有不同的公共假期。
-
您肯定需要一个日历,其中包含域中被视为“周末”、“公共假期”和/或什至“公司假期”的内容(我恰好是一家公司的员工,其中实际上,这是一件事。像我们之间的星期五和星期四的假期这样的“过桥日”是强制性的“不工作”日。)所以,有了这个,你可以计算日期而不考虑日历,然后查看这些非工作日是否属于该范围,然后添加这些天数,然后查看是否有更多非工作日属于添加的范围,冲洗,重复,直到不再是这种情况。跨度>
-
每个国家都有不同的日期格式。
DateTime.TryParse(Console.ReadLine(), out startDate)NOT 使用dd/mm/yyyy,它使用用户区域设置中使用的格式。在美国,它将是mm/dd/yyyy。在俄罗斯dd.mm.yyyy -
要真正计算工作日,您确实需要一个日历。即使在同一国家的不同部门之间,工作日和工作时间也不同。即使在公司之间。
-
@Fildor 感谢指出无效输入的逻辑错误,我会改正的。
标签: c# datetime console-application