C# 将3个开关放在一个开关中

C# 将3个开关放在一个开关中,c#,switch-statement,console-application,C#,Switch Statement,Console Application,一段时间以来,我一直在开发一个控制台日历,它可以独立计算到某个日期,并带有正确的星期日、日期等,这样您就可以在该日期插入数据。所有这些都完成了,但现在我添加了一个函数,您可以将从开始日期到结束日期的日期放入文本文件中。输出类似于: 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.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
这让我进入了程序的mainputsection类,我必须添加参数,以便我可以将消息从“输入年份”等更改为“输入文件的年初”等

现在有三个开关彼此紧挨着,它们做着几乎相同的事情,因此我的程序的这一部分感觉有点简单、重复和硬编码

我想要的是一个循环或者其他东西,它可以缩短这些内容,并通过现在的方式减少它,以便获得正确的
Console.WriteLine()与正确的
控制台.ReadLine()匹配
填写
int[]arrayAnswers={answerYear,answerMonth,answerDay}正确,但更专业。我在想一些类似于for-each循环之类的东西,但我只看到了一半,我需要帮助

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;

    }
}

因此,您需要做的是,将方法中的常见内容分组在一起,在这里,所有情况都应该在控制台中显示一些带有颜色的文本,然后调用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;

}

非常感谢你。这正是我所希望的:)