我一直在试图弄清楚如何根据今天拍摄下一个可用日期,即如果今天是星期五,那么在数组中搜索最近的一天,如果数组值为1 [星期一],2 [星期二] ],4 [星期四],6 [星期六]然后我的第二天应该是星期六。
这是我试过的
//Here i'll get days like 0, 2, 3, 4, 6 pattern, and i'm spliting them based on comma to get single-single day value in array of string
string[] GetDays = DayInWeek.Split(','); [Here day patterns will change everytime, based on user selection]
//Here i'm looping to take each day and get Enum Text based on Enum Value
foreach (string FirstDay in GetDays)
{
//Here i'm converting the string value into int and passing to DayOfWeek Enum to get respective day
DayOfWeek DayChoosen = ((DayOfWeek)(Convert.ToInt32(FirstDay)));
//Here i have my actual day for example Friday
DayOfWeek StartDay = "Friday";
//Here i need condition to find next available day in the foreach i.e., after Friday next value should be Saturday, or Sunday, Monday & so on until Friday==Friday
if (StartDay == DayChoosen)
{
//End foreach
}
}
正如我在今天所说的那样,我应该找到下一个可用日,如果星期五我应该搜索星期六,如果星期六不在那里星期日,星期一等等,直到星期五=星期五
答案 0 :(得分:2)
您不需要foreach
进行所有这些操作。
您可以执行以下操作:
private int nextDay(string dayInWeek, int startDay)
{
int[] getDays = dayInWeek.Split(',').Select(int.Parse).ToArray();
return getDays.Where(d => d > startDay).Any()
? getDays.Where(d => d > startDay).Min()
: getDays.Min();
}
此算法会检查您的数组中是否存在一周中的任何几天,并在startDay
之后。否则,它会在一周内输出第一天。
例如,对于字符串" 0,2,3,4,6":
startDay
0 - 输出2,因为它是大于0的最小整数startDay
1 - 输出2 startDay
3 - output 4 startDay
6,它找不到超过6的项目,并且输出最小为0 对于字符串" 5" (仅限星期五):
startDay
5,6 - 找不到超过5的项目,输出最小(5)startDay
0-4 - 输出5,作为最大数字,大于0-4 答案 1 :(得分:0)
试试这个;
//Here i'll get days like 0, 2, 3, 4, 6 pattern, and i'm splitting them based on comma to get single-single day value in array of string
string DayInWeek = "0, 2, 3, 4, 6";
string[] GetDays = DayInWeek.Split(','); //[Here day patterns will change everytime, based on user selection]
DayOfWeek nextAvailableDay;
//Here i'm looping to take each day and get Enum Text based on Enum Value
foreach (string FirstDay in GetDays)
{
//Here i'm converting the string value into int and passing to DayOfWeek Enum to get respective day
DayOfWeek DayChoosen = ((DayOfWeek)(Convert.ToInt32(FirstDay)));
//Here i have my actual day for example Friday
DayOfWeek StartDay = DayOfWeek.Friday;
//Here i need condition to find next available day in the foreach i.e., after Friday next value should be Saturday, or Sunday, Monday & so on until Friday==Friday
if (StartDay.Equals(DayChoosen))
break;
if (StartDay < DayChoosen)
{
nextAvailableDay = DayChoosen;
break;
}
continue;
}
答案 2 :(得分:0)
你应该在int
列表上玩。我会为你提供伪代码。
<强>算法:强>
这是我提出的解决此问题的可能算法。您可以将其转换为代码。可能它不是最好的,但我相信它会正常工作。