2014-02-10 36 views
0

因爲即時通訊新的C#,即使我有編碼在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(); 
    } 
+0

我很困惑你的問題是什麼?你做了這件事。 – Shahar

+0

第一次執行上面的程序後,我希望它提示要求用戶輸入m,y,n(月份,年份,要打印的月份數)的部分。這應該重複,直到輸入0爲止的月數。我希望這是明確的 –

+0

在我看來,如果(月> 12)應該進入for循環。此外,當按下回車鍵時,您可以停止詢問更多信息。看到我的答案... –

回答

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; 
    } 
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]); 
    } 
} 

讓我知道這是不是你的意思。

+0

這個工程,但是當我輸入0的月數,它不會終止/關閉命令提示符窗口,而是詢問用戶的m,y,n –

+0

@Abner_CV哦,我以爲你的意思是,如果你輸入文字「0」,那麼它就結束了。讓我解決它。 – Shahar

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]); 

     ... 
     ... 
    } 
} 
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();