0

因为我是 C# 的新手,即使我以前用 C 编写过代码,我仍然有一个问题,关于如何在程序已经运行后执行我要求用户输入一组输入的部分。

以下程序打印具有指定月数的日历,但我需要帮助编写另一行代码,在用户已经输入值一次后,要求用户输入要打印的月、年和月数. 我是否必须在我的主函数中进行某种类型的循环,或者我是否必须在我的主函数上方的方法中进行?

static void GenMonth(int month, int year)
    {
        int daycode, ndim;
        PrintHeader(month, year);
        ndim=GetNDIM(month,year);
        int day=1;
        daycode = GetDayCode(month, day, year);
        int a,i;
        for(a=1;a<=daycode;a++)
        {
             Console.Write("    ");
        }
        for (i = 1; i <= GetNDIM(month, year); i++)
        {

           Console.Write("{0,4}", i);
           if (((i + daycode) % 7) == 0)
               Console.Write("\n");

        }
       daycode = GetDayCode(month, day, year); 
        if (daycode == 6) 
        {
            Console.Write("\n");
        } 

    }
    static void Main(string[] args)
    {
        Console.WriteLine("please enter m,y,n: \n");
        string input = Console.ReadLine();
        string[] split = input.Split(' ');
        int month = Int32.Parse(split[0]);
        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);
        int i=0;
        for (i = 0; i < numberOfMonths; i++)
        {
            GenMonth(month, year);
            month++;
            Console.Write("\n \n");
        }
        if (month > 12)
        {
            month = 1;
            year++;
        }
        Console.ReadKey();
    }
4

4 回答 4

1

您可能会得到几种方法来做到这一点 - 这是一种可能性。0只需连续循环,然后在检测到月份的值时退出循环(程序将终止) 。

static void Main(string[] args)
{
    int month = -1;

    while (true)
    {
        Console.WriteLine("please enter m,y,n: \n");
        string input = Console.ReadLine();
        string[] split = input.Split(' ');
        month = Int32.Parse(split[0]);

        if (month == 0)
            break;

        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);

        ...
        ...
    }
}
于 2014-02-10T01:51:56.597 回答
0

试试这个:

    static void Main(string[] args)
    {
        while (AskForDate())
        {}
    }

    private static bool AskForDate()
    {
        Console.WriteLine("please enter m,y,n: \n");
        string input = Console.ReadLine();
        string[] split = input.Split(' ');
        int month = Int32.Parse(split[0]);
        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);
        int i = 0;
        for (i = 0; i < numberOfMonths; i++)
        {
            GenMonth(month, year);
            month++;
            Console.Write("\n \n");
        }
        if (month > 12)
        {
            month = 1;
            year++;
        }

        Console.Out.WriteLine("Again? [Y/n]");

        var key = Console.ReadKey();
        return key.Key != ConsoleKey.N;
    }
于 2014-02-10T01:51:30.050 回答
0
for(;;)
{
    Console.WriteLine("Enter: Month Year NumberOfMonths\nPress enter to stop.");
    string line = Console.ReadLine();
    if (line == "")
        break;
    string[] terms = line.Split();
    int
        month = int.Parse(terms[0]),
        year = int.Parse(terms[1]),
        numberOfMonths = int.Parse(terms[2]);
    for (int i = 0; i < numberOfMonths; i++)
    {
        GenMonth(month, year);
        if (month == 12)
        {
            month = 1;
            year++;
        }
        else
            month++;
    }
}

Console.Write("\nPress any key...");
Console.ReadKey();
于 2014-02-10T03:21:57.037 回答
0

只需使用 while 子句:

static void Main(string[] args)
{
    Console.WriteLine("please enter m,y,n: \n");
    string input = Console.ReadLine();
    string[] split = input.Split(' ');
    int month = Int32.Parse(split[0]);
    while (month != 0)
    {
        int year = Int32.Parse(split[1]);
        int numberOfMonths = Int32.Parse(split[2]);
        int i=0;
        for (i = 0; i < numberOfMonths; i++)
        {
            GenMonth(month, year);
            month++;
            Console.Write("\n \n");
        }
        if (month > 12)
        {
            month = 1;
            year++;
        }
        Console.ReadKey();
        Console.WriteLine("please enter m,y,n: \n");
        input = Console.ReadLine();
        split = input.Split(' ');
        month = Int32.Parse(split[0]);
    }
}

如果这不是你的意思,请告诉我。

于 2014-02-10T01:51:44.460 回答