-1

how do I use an integer value to determine the current day of the week and another integer number to determine the next day of the week? For example if today is (Enter an integer like 0 for sunday), and I put the integer 2, then its tuesday. Here's the code I have so far:

namespace DayOfTheWeek
{
    class Program
    {
        static void Main(string[] args)
        {
            int Sunday=0;
            int Monday = 1;
            int Tuesday = 2;
            int Wednesday = 3;
            int Thursday = 4;
            int Friday = 5;
            int Saturday = 6;

            Console.WriteLine("Today is " + 0);
        }
    }
}

I'm really stumped.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • i assume you want to print out ""Today is Tuesday" or the equivalent? – kei Sep 23 '19 at 04:48
  • 1
    I'd suggest using a string array for this... ["Sunday", "Monday", etc]. You don't need individual variables. You can access the day of the week from the index – OneCricketeer Sep 23 '19 at 04:50
  • See also related questions https://stackoverflow.com/questions/29502210/how-to-get-from-an-integer-day-value-day-as-string, https://stackoverflow.com/questions/727173/c-sharp-3-0-how-can-i-order-a-list-by-week-name-starting-on-monday, https://stackoverflow.com/questions/34349845/find-next-available-day-in-an-array-of-string – Peter Duniho Sep 23 '19 at 06:06

1 Answers1

5

You could use an array for this:

string[] daysOfWeek = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int dayVal = 2;
if (dayVal >= 0 && dayVal < daysOfWeek.Length) // check it's within the bounds of the array, we don't want any IndexOutOfBounds exceptions being thrown for bad user input
{
    Console.WriteLine($"Today is {daysOfWeek[dayVal]}.")
}

Alternatively, you could use a dictionary:

Dictionary<int, string> daysOfWeek = new Dictionary<int, string>()
{
    { 0, "Sunday" },
    { 1, "Monday" },
    { 2, "Tuesday" },
    { 3, "Wednesday" },
    { 4, "Thursday" },
    { 5, "Friday" },
    { 6, "Saturday" }
};

int dayVal = 2;
if (daysOfWeek.TryGetValue(dayVal, out string dayName)) // check it's in the dictionary and retrieve the result
{
    Console.WriteLine($"Today is {dayName}.");
}

Or you could create an enum (for days of the week you don't need to since .NET already has such an enum):

public enum DaysOfWeek
{
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6
}

int dayVal = 2;
if (Enum.IsDefined(typeof(DaysOfWeek), dayVal)) // check it's in the enum
{
    var dayOfWeek = (DaysOfWeek)dayVal;
    Console.WriteLine($"Today is {dayOfWeek}."); // this will print the name corresponding to the enum value
}

And to instead use the built-in .NET DayOfWeek enum:

int dayVal = 2;
if (Enum.IsDefined(typeof(DayOfWeek), dayVal)) // check it's in the enum
{
    var dayOfWeek = (DayOfWeek)dayVal;
    Console.WriteLine($"Today is {dayOfWeek}."); // this will print the name corresponding to the enum value
}

To determine the next day of the week you should use the following:

int nextDay = dayVal == 6 ? 0 : dayVal + 1;

Note that an alternative approach is the following:

DateTime today = DateTime.Now.Date;
Console.WriteLine($"Today is {today.DayOfWeek}.");
DateTime tomorrow = today.AddDays(1);
Console.WriteLine($"Tomorrow is {tomorrow.DayOfWeek}.");
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86