需要帮助在C#中第二次询问用户输入

时间:2014-02-10 01:36:49

标签: c# loops input console.readline

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

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

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 个答案:

答案 0 :(得分: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]);

        ...
        ...
    }
}

答案 1 :(得分: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;
    }

答案 2 :(得分: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]);
    }
}

如果那不是您的意思,请告诉我。

答案 3 :(得分: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();