c#如何在控制台中打印字符串数组?

时间:2015-11-25 19:16:32

标签: c# arrays string printing console

粗体文字是我需要改变但不确定的地方。我希望控制台将月份显示为一个字符串,例如' Jul' '日及39#; ... 基本上我对案例1的编码显示= 1/1/2001 和案例3我希望它显示= 1 / Jan / 2001

static void Main(string[] args)
{

    do
    {
        Console.WriteLine("Please select a date format by entering either the 1, 2 or 3 Key");
        Console.WriteLine(" 1 = dd/mm/yyyy ");
        Console.WriteLine(" 2 = mm/dd/yyyy ");
        Console.WriteLine(" 3 = dd/mmm/yyyy ");
    } while (!int.TryParse(Console.ReadLine(), out f) || f < 0 || f>3);

    switch(f)
    {
        case 1:
            getDate1();
            Console.WriteLine("The day after {0}/{1}/{2} is {3}", d, m, y, md.NextDay1());
            break;
        case 2:
            getAmericanDate();
            Console.WriteLine("The day after {1}/{0}/{2} is {3}", d, m, y, md.NextDay1());
            break;
        **case 3:
            getAbbreviatedMonth();
            Console.WriteLine("The day after {1}/{4}/{2} is {3}", d, m, y, md.NextDay1());
            break;**

    }


static void getAbbreviatedMonth() //dd/mmm/yyyy
{

    do
    {
        Console.Write("PLease enter the year (not earlier than 1812) as 4 digits  >> ");
    } while (!int.TryParse(Console.ReadLine(), out y) || y < 1812);


    do
    {
        Console.Write("Please enter the month as a three letter character ( e.g 'Jul')   >> ");
    } while (isCorrectMonth(Console.ReadLine()));


    do
    {

        Console.Write("Please enter the day as a whole between 1 & {0} >> ", DayInMonth(m, y));
    } while (!int.TryParse(Console.ReadLine(), out d) || d > DayInMonth(m, y) || d < 1);



    md = new myDate(d, m, y);
}


static bool isCorrectMonth(string monthToCheck)
{
    string stringToCheck = monthToCheck.ToLower();
    string[] stringArray = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
    foreach (string x in stringArray)
    {
        if (x.Contains(stringToCheck))
        {
            // Process..
            return true;
        }
        else
        {
            return false;
        }
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

Console.WriteLine("The day after {1}/{4}/{2} is {3}", d, m, y, md.NextDay1())

这里你的论点应该是0-3而不是1-4。

您可能会发现将输入解析为DateTime更容易,然后可以轻松地以各种格式输出 - 您可以通过调用.AddDays(1)找到第二天。

答案 1 :(得分:0)

请参阅此示例以格式化日期:

DateTime time = DateTime.Now;             // Use current time.
string format = "MMM ddd d HH:mm yyyy";   // Use this format.
Console.WriteLine(time.ToString(format)); // Write to console.

输出是:

Feb Fri 27 11:41 2009

来源为http://www.dotnetperls.com/datetime-format