我有一段时间以来一直在制作一个控制台日历,这个日历依赖于某个日期与正确的星期几,日期等,然后允许您在当天插入数据。所有这一切都已完成,但现在我添加了一个函数,您可以将日期从开始日期到结束日期放入文本文件中。输出将类似于:
05.06.2016 Tuesday
06.06.2016 Wednesday
07.06.2016 Thursday
01.06.2016 Friday
02.06.2016 Saturday
03.01.2016 Sunday
04.01.2016 Monday
05.01.2016 Tuesday
06.01.2016 Wednesday
07.01.2016 Thursday
01.01.2016 Friday
02.01.2016 Saturday
这将我发送到我程序的这个MainInputSection类,我不得不添加参数,以便我可以改变来自"输入YEAR"等等#34;输入YEAR文件"等
现在有三个开关彼此相邻,几乎完全相同,因此我的程序的这一部分感觉有点简单,重复和硬编码。
我喜欢的是一个循环或缩短这些内容的东西,并使它现在的方式不那么no,以便使正确Console.WriteLine();
与正确的Console.ReadLine();
匹配正确地填写int[] arrayAnswers = { answerYear, answerMonth, answerDay };
,但更专业。我正在为每个循环或其他东西思考一个类似的东西,但我只看到一个这样的解决方案,我需要帮助。
public class MainInputSection
{
public static int[] GetUserInputDate(string mode)
{
int answerYear;
int answerMonth;
int answerDay;
Console.ForegroundColor = ConsoleColor.Cyan;
switch (mode)
{
case "calender":
Console.WriteLine("Input YEAR");
break;
case "fileStart":
Console.WriteLine("Input start of YEAR for file");
break;
case "fileEnd":
Console.WriteLine("Input end of YEAR for file");
break;
}
Console.ResetColor();
answerYear = Convert.ToInt32(Console.ReadLine());
Console.ForegroundColor = ConsoleColor.Cyan;
switch (mode)
{
case "calendar":
Console.WriteLine("Input MONTH");
break;
case "fileStart":
Console.WriteLine("Input start of MONTH for file");
break;
case "fileEnd":
Console.WriteLine("Input end of MONTH for file");
break;
}
Console.ResetColor();
answerMonth = Convert.ToInt32(Console.ReadLine());
Console.ForegroundColor = ConsoleColor.Cyan;
switch (mode)
{
case "calendar":
Console.WriteLine("Input DAY");
break;
case "fileStart":
Console.WriteLine("Input start of DAY for file");
break;
case "fileEnd":
Console.WriteLine("Input end of DAY for file");
break;
}
Console.ResetColor();
answerDay = Convert.ToInt32(Console.ReadLine());
int[] arrayAnswers = { answerYear, answerMonth, answerDay };
return arrayAnswers;
}
}
答案 0 :(得分:0)
所以你需要做的是,在一个方法中将常见的东西组合在一起,这里所有的情况都应该在控制台中用颜色显示一些文本,然后调用reset color选项,并将用户输入存储到整数变量。因此,将它们组合在一个方法中并从方法返回整数值。我认为该方法的签名应如下所示:
public static int GetInput(string displayMessage)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(displayMessage);
Console.ResetColor();
return int.Parse(Console.ReadLine());
}
您可以使用以下方法:
public static int[] GetUserInputDate(string mode)
{
int answerYear = 0;
int answerMonth = 0;
int answerDay = 0;
switch (mode)
{
case "calender":
answerYear = GetInput("Input YEAR");
answerMonth = GetInput("Input MONTH");
answerDay = GetInput("Input DAY");
break;
case "fileStart":
answerYear = GetInput("Input start of YEAR for file");
answerMonth = GetInput("Input start of MONTH for file");
answerDay = GetInput("Input start of DAY for file");
break;
case "fileEnd":
answerYear = GetInput("Input end of YEAR for file");
answerMonth = GetInput("Input end of MONTH for file");
answerDay = GetInput("Input end of DAY for file");
break;
}
int[] arrayAnswers = { answerYear, answerMonth, answerDay };
return arrayAnswers;
}