Reputation: 11
i am strugling with an exercise. I am a beginer and dont know where to start. I am asked to create a console program that if you give it a number between 1 and 12 it must give you the corresponding month name and if you give it the name of the month it should give you the number of the month. Please help with the code. It should be done using an ARRAY. Thank you.
Upvotes: 0
Views: 2084
Reputation: 3049
private string[] months = { "Jan", "Feb", "Mar", "Apr" };
public string GetMonth(int x)
{
if(x > 0 && x < months.Length)
return months[x];
else
return "";
}
Upvotes: 0
Reputation: 7342
Hopefully you will learn something from this code, because if it gets copied to a USB stick and givent to the teacher without even taking alook to it, I will be very mad, come to your home and do a mess! :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int iMonth = -1;
// loop until iMonth is 0
while (iMonth != 0)
{
Console.WriteLine("Please insert a number from 1 to 12 and press enter. Enter 0 to exit.");
string sMonth = Console.ReadLine();
// try to get a number from the string
if (!int.TryParse(sMonth, out iMonth))
{
Console.WriteLine("You did not enter a number.");
iMonth = -1; // so we continue the loop
continue;
}
// exit the program
if (iMonth == 0) break;
// not a month
if (iMonth < 1 || iMonth > 12) {
Console.WriteLine("The number must be from 1 to 12.");
continue;
}
// get the name of the month in the language of the OS
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(iMonth);
Console.WriteLine("The name of the month is " + monthName);
}
}
}
}
If your teacher expects a custom provided name then you can use the switch statement in the last part:
switch (iMonth)
{
case 1:
Console.WriteLine("January");
break;
case 2:
Console.WriteLine("February");
break;
// add more
}
If he expects an array exercise then you can declare an array with all the strings and use that:
string[] monthNames = new string[] {
"January",
"February",
// add more
};
and use this to get the name:
Console.WriteLine(monthNames[iMonth]);
Upvotes: 2
Reputation: 700382
The task of translating between numbers and names is quite trivial, so how you do it depends on what kind of language elements you are currently learning.
You can divide the problem into several sub-task, like determining if the input is a number of a name, picking the right conversion based on that, and the two different ways of doing the conversion. Each sub-task can be solved in several different ways.
To examine the input you could compare it to month names, and if none of them matches assume that it's a number, or you could use Int32.TryParse
to try to parse the input as a number, and if that fails assume that it's a month name.
The most basic way of doing the conversion would be a lot of if
statements. You could also use a switch, use an array of month names, or use dictionaries for the separate lookups.
Upvotes: 0
Reputation: 3374
static void Main(string[] args)
{
Console.WriteLine("Give me an integer between 1 and 12, and I will give you the month");
int monthInteger = int.Parse(Console.ReadLine());
DateTime newDate = new DateTime(DateTime.Now.Year, monthInteger, 1);
Console.WriteLine("The month is: " + newDate.ToString("MMMM"));
Console.WriteLine();
Console.WriteLine("Give me a month name (ex: January), and I will give you the month integer");
string monthName = Console.ReadLine();
monthInteger = DateTime.ParseExact(monthName + " 1, " + DateTime.Now.Year, "MMMM d, yyyy", System.Globalization.CultureInfo.InvariantCulture).Month;
Console.WriteLine("The month integer is " + monthInteger);
Console.ReadLine();
}
Upvotes: 0
Reputation: 16839
As Ernest suggested, take a look at the DateTimeFormat property within System.Globalization.CultureInfo. What you're looking for is a method called GetMonthName(). The number passed into GetMonthName() is a numerical representation of that month.
Upvotes: 0
Reputation: 3106
Do like that :
String to Int Month
Code :
Datetime.Parse(Monvalue + "/1/2011").ToString("MM")
Like :
Datetime.Parse("January/1/2011").ToString("MM")
Retrun : 01
Int to String Month
Code :
Datetime.Parse( Monvalue +"/9/2011").ToString("MMMMM")
Like :
Datetime.Parse("1/9/2011").ToString("MMMMM")
Retrun : "January"
Before this you should handle wrong cases.I hope its help to you
Upvotes: 0
Reputation: 18704
Depends on what you're learning, I guess... they may just be demonstrating a switch(intMonth) type thing, so:
switch(intMonth)
{
case 1:
return "January";
break;
case 2:
return "February";
break;
....
}
Or as mentioned, make use of DateTime...
There's many many ways to do it... I guess you need to select the right way... most efficient way... so, depends on your assignment.
Good luck.
Upvotes: 2