问题
I've been trying to figure out how to take next available day based on Present day i.e., if today is Friday, then search in Array for the next nearest day like if Array values are 1[Monday], 2[Tuesday], 4[Thursday], 6[Saturday] then my next day should be Saturday.
Here is what i tried
//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
}
}
As i told based on present Day i should find next available day i.e, if Friday i should search for Saturday, if Saturday is not there then Sunday, Monday and so on till Friday=Friday
回答1:
You don't need all these manipulations with foreach
.
You can do the following:
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();
}
This algorithm checks if there are any days of a week, which are presented in your array, and come after the startDay
. Else, it outputs the first available day in a week.
For example, for a string "0, 2, 3, 4, 6":
- for
startDay
0 - output 2, as it is the minimal integer which is more than 0 - for
startDay
1 - outputs 2 - for
startDay
3 - output 4 - for
startDay
6 it finds no items, which are more than 6, and outputs minimum 0
For string "5" (Friday only):
- for
startDay
5, 6 - finds no items which are more than 5, output minimum (5) - for
startDay
0-4 - outputs 5, as the minimum number which is greater than 0-4
回答2:
Try this;
//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;
}
回答3:
You should play on int
list. I will provide you the pseudo code.
Algorithm:
- sort the available list
- get all greater numbers (greater than your current one) from the list
- select the minimum now, and break
- if you don't have any greater number, select all the minimum numbers (less than your current one)
- select maximum from this list now, and break
- if you don't have any greater or less numbers, than select the same number and break
- now convert the number into week day
It is the possible algorithm I came up to solve this problem. You can convert it into code. May be its not the best one but I am sure it will work fine.
回答4:
check this:
Console.WriteLine("Kindly provide input");
string DayInWeek = Console.ReadLine();
string[] GetDays = DayInWeek.Split(',');
Array.Sort(GetDays);
DateTime dt = DateTime.Today;
int i = (int)dt.DayOfWeek;
Console.WriteLine("Today is " + (DayOfWeek)i);
bool flag = false;
foreach (string FirstDay in GetDays)
{
if (Convert.ToInt32(FirstDay) > i)
{
Console.WriteLine((DayOfWeek)Convert.ToInt32(FirstDay) + " is the next day");
flag = true;
break;
}
}
if (!flag)
{
Console.WriteLine((DayOfWeek)Convert.ToInt32(GetDays[0]) + " is the next day");
}
Console.ReadKey();
来源:https://stackoverflow.com/questions/34349845/find-next-available-day-in-an-array-of-string