HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Switch case reviews, finding days of the week that coresponds to a number given

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
casereviewsthenumbercorespondsweekthatfindingswitchgiven

Problem

Given a number between 1 and 7, this will display which day of the week corresponds to that number, starting with Monday. This is pretty simple and pretty easy, but it seems that there would be a much better way to do this, where I wouldn't have to write out each day of the week, and possibly iterate through an array..?

```
using System;

namespace FindDay
{
class Program
{
static void Main(string[] args)
{
//This program will provide you the day of the week
//through a given number that coresponds to that day
//of the week, IE: Monday => 1st day of the week

int num;
string result;

label:

Console.Write("Enter a number between 1 and 7: ");
num = Convert.ToInt32(Console.ReadLine());

switch (num)
{
case 1:
result = "Monday";
Console.WriteLine("{0} => {1}st day of the week", result, num);
break;
case 2:
result = "Tuesday";
Console.WriteLine("{0} => {1}nd day of the week", result, num);
break;
case 3:
result = "Wednesday";
Console.WriteLine("{0} => {1}rd day of the week", result, num);
break;
case 4:
result = "Thursday";
Console.WriteLine("{0} => {1}th day of the week", result, num);
break;
case 5:
result = "Friday";
Console.WriteLine("{0} => {1}th day of the week", result, num);
break;
case 6:
result = "Saturday";
Console.WriteLine("{0} => {1}th day of the week", result, num);
break;
case 7:
result = "Sunday";
Console.Wri

Solution

I would add to forsvarir's good answer: start thinking now about the power of abstraction. Programming is all about building abstractions. Make your abstractions solve one problem and solve it well. For instance, here's a problem to solve:

static string Ordinal(int n)
{
    // You fill this in. The method takes an integer and returns
    // 1st, 2nd, 3rd, 4th, etc. 
}


Heres another one:

static string WeekdayName(int w)
{
  // To do: Return the name of the day of the week.
}


And here's another:

static int GetIntegerFromConsole(int low, int high)
{
    // to do: prompt the user, sit in a loop until they give
    // a response in the right range, and return it.
}


Once you have those methods then your program becomes trivial.

int weekdayNumber = GetIntegerFromConsole(1, 7);
string weekdayName = WeekdayName(weekdayNumber);
string ordinal = Ordinal(weekdayNumber);
Console.WriteLine($"{weekdayName} => {ordinal} day of the week");


You know what I like? Four-line-long programs, that's what I like. Move the mechanisms to their own helper methods, debug those methods, and then rely on them to do their jobs.

Code Snippets

static string Ordinal(int n)
{
    // You fill this in. The method takes an integer and returns
    // 1st, 2nd, 3rd, 4th, etc. 
}
static string WeekdayName(int w)
{
  // To do: Return the name of the day of the week.
}
static int GetIntegerFromConsole(int low, int high)
{
    // to do: prompt the user, sit in a loop until they give
    // a response in the right range, and return it.
}
int weekdayNumber = GetIntegerFromConsole(1, 7);
string weekdayName = WeekdayName(weekdayNumber);
string ordinal = Ordinal(weekdayNumber);
Console.WriteLine($"{weekdayName} => {ordinal} day of the week");

Context

StackExchange Code Review Q#128781, answer score: 19

Revisions (0)

No revisions yet.