こんにちは、ユーザーが月の数を入力すると、アプリはそれが何月であるかを判断し、回答を表示し、月の日数を表示するコンソールアプリを作成しています。switch ステートメントが Main メソッドにあるときに機能していましたが、今では間違った月が表示されます。次のコードがあります。
namespace ConsoleCA2{
public enum Months
{
January = 31,
February = 28,
March = 31,
April = 30,
May = 31,
June = 30,
July = 31,
August = 31,
September = 30,
October = 31,
November = 30,
December = 31
};
public class Program
{
public const string NEWLINE = "\n"; //constant for new line
static void Main(string[] args)
{
try
{
//prompt user for month number.
Console.Write("Enter a Number to find out what Month it is: ");
//convert users answer to integer
int userInput = Convert.ToInt16(Console.ReadLine());
//declare and initalise
int daysInMonth = 0;
string answer = String.Empty;
//call relevent calculation methods
answer = DetermineMonth(userInput);
daysInMonth = DetermineDaysInMonth(userInput);
//Write users answer to screen
Console.WriteLine(answer);
//Prompt user, whether they want to know number of days in the
//month
Console.Write("Would you like to know the number of days in" +
" {0}: ", answer);
//assign users answer to variable
string userAnswer = Console.ReadLine();
//Output answer if needed.
if (userAnswer == "yes".Trim().ToLower())
{
Console.WriteLine("The number of days in {0} is: {1}",
answer, daysInMonth.ToString());
}
else if (userAnswer == "no".Trim().ToLower())
{
Console.WriteLine(NEWLINE);
Console.WriteLine("You answered No, Have a good day!");
}
else
{
Console.WriteLine("Please answer yes or no.");
}
Console.Read(); //Pauses screen for user
}
//catch any errors.
catch (Exception err)
{
Console.WriteLine("Error" + err.Message);
}
}
/// <summary>
/// Determines What month the use enters and returns string value
/// </summary>
/// <param name="userInput"></param>
/// <returns>returns the month as string</returns>
public static String DetermineMonth(int userInput)
{
switch (userInput)
{
case 1:
return Months.January.ToString();
case 2:
return Months.February.ToString();
case 3:
return Months.March.ToString();
case 4:
return Months.April.ToString();
case 5:
return Months.May.ToString();
case 6:
return Months.June.ToString();
case 7:
return Months.July.ToString();
case 8:
return Months.August.ToString();
case 9:
return Months.September.ToString();
case 10:
return Months.October.ToString();
case 11:
return Months.November.ToString();
case 12:
return Months.December.ToString();
default:
return "Error";
}
}
/// <summary>
/// Determines how many days is in selected month as integer
/// </summary>
/// <param name="userInput"></param>
/// <returns>returns how many days in month</returns>
public static int DetermineDaysInMonth(int userInput)
{
switch (userInput)
{
case 1:
return Convert.ToInt16(Months.January);
case 2:
return Convert.ToInt16(Months.February);
case 3:
return Convert.ToInt16(Months.March);
case 4:
return Convert.ToInt16(Months.April);
case 5:
return Convert.ToInt16(Months.May);
case 6:
return Convert.ToInt16(Months.June);
case 7:
return Convert.ToInt16(Months.July);
case 8:
return Convert.ToInt16(Months.August);
case 9:
return Convert.ToInt16(Months.September);
case 10:
return Convert.ToInt16(Months.October);
case 11:
return Convert.ToInt16(Months.November);
case 12:
return Convert.ToInt16(Months.December);
default:
return 0;
}
}
古い switch ステートメントは次のとおりです。
switch (userInput)
{
case 1:
answer = Months.January.ToString();
daysInMonth = Convert.ToInt16(Months.January);
break;
case 2:
answer = Months.February.ToString();
daysInMonth = Convert.ToInt16(Months.February);
break;
case 3:
answer = Months.March.ToString();
daysInMonth = Convert.ToInt16(Months.March);
break;
case 4:
answer = Months.April.ToString();
daysInMonth = Convert.ToInt16(Months.April);
break;
case 5:
answer = Months.May.ToString();
daysInMonth = Convert.ToInt16(Months.May);
break;
case 6:
answer = Months.June.ToString();
daysInMonth = Convert.ToInt16(Months.June);
break;
case 7:
answer = Months.July.ToString();
daysInMonth = Convert.ToInt16(Months.July);
break;
case 8:
answer = Months.August.ToString();
daysInMonth = Convert.ToInt16(Months.August);
break;
case 9:
answer = Months.September.ToString();
daysInMonth = Convert.ToInt16(Months.September);
break;
case 10:
answer = Months.October.ToString();
daysInMonth = Convert.ToInt16(Months.October);
break;
case 11:
answer = Months.November.ToString();
daysInMonth = Convert.ToInt16(Months.November);
break;
case 12:
answer = Months.December.ToString();
daysInMonth = Convert.ToInt16(Months.December);
break;
default:
answer = "Error";
break;
}
別の方法に変更するまではうまくいきましたが、そこから下り坂になりました。元に戻しても、まだ月が混同されますか?
とても混乱しました:S
助けてくれてありがとう...乱雑なコードを理解できるなら:p