2016-12-01 4 views
1

私はこれに固執している、私はどのような年と月を入力するユーザー入力を求めることができますか、それは指定された年と月の毎月のカレンダーを出力します、私もsystem.datetimeを使用する必要があります これは私が今まで持っていると私はそれが正しいと思いますコードは、任意の助けに感謝します。おかげC# - カレンダーを表示し、年と月を尋ねるプログラムを書く、System.DateTimeを使用する

class Program 
{ 
    static int year = new int(); 
    static int month = new int(); 
    static int[,] calendar = new int[6, 7]; 

    static void Main(string[] args) 
    { 
     Console.Write("Enter the year? "); 
     year = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter the month (January = 1, etc): "); 
     month = Convert.ToInt32(Console.ReadLine()); 

     DrawHeader(); 
     FillCalendar(); 
     DrawCalendar(); 
     Console.ReadLine(); 
    } 

    static void DrawHeader() 
    { 
     Console.Write("\n\n"); 
     Console.WriteLine("\t" + month); 
     Console.WriteLine("Mo Tu We Th Fr Sa Su"); 
    } 

    static void FillCalendar() 
    { 
     int days = DateTime.DaysInMonth(year, month); 
     int currentDay = 1; 
     for (int i = 0; i < calendar.GetLength(0); i++) 
     { 
      for (int j = 0; j < calendar.GetLength(1) && currentDay <= days; j++) 
      { 
       if (i == 0 && month > j) 
       { 
        calendar[i, j] = 0; 
       } 
       else 
       { 
        calendar[i, j] = currentDay; 
        currentDay++; 
       } 
      } 
     } 
    } 

    static void DrawCalendar() 
    { 
     for (int i = 0; i < calendar.GetLength(0); i++) 
     { 
      for (int j = 0; j < calendar.GetLength(1); j++) 
      { 
       if (calendar[i, j] > 0) 
       { 
        if (calendar[i, j] < 10) 
        { 
         Console.Write(" " + calendar[i, j] + " "); 
        } 
        else 
        { 
         Console.Write(calendar[i, j] + " "); 
        } 
       } 
       else 
       { 
        Console.Write(" "); 
       } 
      } 
      Console.WriteLine(""); 
     } 
    } 
} 
+0

なぜあなたはそれが間違っていると思いますか詳細を少し詳しく説明できますか?あなたが遭遇した正確な問題は何ですか? –

+0

System.DateTimeを使用する必要があるということは何のために使用する必要があるのですか?例えば。あなたは日付オブジェクトに入力された値を変換する必要がありますか? –

+0

yh、その意味で正しいとは言えません。たとえば、system.datetimeを使用する必要があります。私はそれをどのように実装できるか分かりません。 –

答えて

0

これは、月の日付が正しく曜日と一致していることを確認する必要があります(つまり、12月1日は火曜日)

using System; 
using System.Globalization; 

namespace Calendar 
{ 
class Program 
{ 
    static int year = new int(); 
    static int month = new int(); 
    static int[,] calendar = new int[6, 7]; 
    private static DateTime date; 

    static void Main(string[] args) 
    { 
     Console.Write("Enter the year? "); 
     year = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter the month (January = 1, etc): "); 
     month = Convert.ToInt32(Console.ReadLine()); 

     date = new DateTime(year, month, 1);//gives you a datetime object for the first day of the month 
     DrawHeader(); 
     FillCalendar(); 
     DrawCalendar(); 
     Console.ReadLine(); 
    } 

    static void DrawHeader() 
    { 
     Console.Write("\n\n"); 
     //gives you the month and year at the top of the calendar 
     Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) + " " + year); 
     Console.WriteLine("Mo Tu We Th Fr Sa Su"); 
    } 

    static void FillCalendar() 
    { 
     int days = DateTime.DaysInMonth(year, month); 
     int currentDay = 1; 
     var dayOfWeek = (int) date.DayOfWeek; 
     for (int i = 0; i < calendar.GetLength(0); i++) 
     { 
      for (int j = 0; j < calendar.GetLength(1) && currentDay - dayOfWeek + 1 <= days; j++) 
      { 
       if (i == 0 && month > j) 
       { 
        calendar[i, j] = 0; 
       } 
       else 
       { 
        calendar[i, j] = currentDay - dayOfWeek + 1; 
        currentDay++; 
       } 
      } 
     } 
    } 

    static void DrawCalendar() 
    { 
     for (int i = 0; i < calendar.GetLength(0); i++) 
     { 
      for (int j = 0; j < calendar.GetLength(1); j++) 
      { 
       if (calendar[i, j] > 0) 
       { 
        if (calendar[i, j] < 10) 
        { 
         Console.Write(" " + calendar[i, j] + " "); 
        } 
        else 
        { 
         Console.Write(calendar[i, j] + " "); 
        } 
       } 
       else 
       { 
        Console.Write(" "); 
       } 
      } 
      Console.WriteLine(""); 
     } 
    } 
} 

}

EDIT

選択した月の特定の曜日のすべての日付をすべて取得したい場合は、このようなことができます。

 var weekDay = 0; 
     var dates = new List<DateTime>(); 
     for (int i = 0; i < days; i++) 
     { 
      if ((int)date.AddDays(i).DayOfWeek == weekDay) 
      { 
       dates.Add(date.AddDays(i)); 
      } 
     } 

曜日を入力してweekDayに入力する値を入力するようにユーザーに求めなければなりません。日曜日が0、土曜日が6であることを覚えておいてください。(私はこれを十分にテストしていませんので注意してください) - これはあなたのFillCalendarメソッドに入力する必要があります。

+0

okありがとう、助けてくれて、それが私の後ろにあるかどうか見てみましょう –

+0

今日は(2016年の12月1日)月曜日です。今日は木曜日なので、そうではありません。 –

+0

日付は正しく表示されていません。たとえば、2016年と11月の日付が間違って12月の場合はどうすればいいですか? –

関連する問題