2016-04-06 6 views
0

私は、教育的な仕事(学校)としてコンソールベースのカレンダーに取り組んでいます。ユーザーが入力した特定の月のカレンダーシートを表示する必要があります。私はほとんどそこにいるが、今月はすべて月曜日に始まることに気づいた。私はstringbuilderを使ってシートを作成しました。月の最初の日に実際にゼロが来る前に日数を埋めるようにしましたが、まだ動作しません。 forループは私の試みです。誰もがアイデアを持っていますか?まだ勉強してる。ここではその問題とは別に、正常に動作しますwhichsシートを、構築するコードは次のとおりです。Consolebuilder with stringbuilderカレンダーシート

DateTime date = DateTime.Now; 
     List<DateTime> dateList = new List<DateTime>(); 

     DateTime dateCopyForModification = date; 

     int inputMonth = date.Month; 

     while (dateCopyForModification.Month == inputMonth) 
     { 
      dateList.Add(dateCopyForModification); 
      dateCopyForModification = dateCopyForModification.AddDays(1); 
     } 

     Console.WriteLine("\t---------\n\t{0}, {1}\n\t---------", date.ToString("MMMM"), date.Year); 

     int dayCounter = 0; 
     StringBuilder stringBuilder = new StringBuilder(); 

     stringBuilder.Append("\nMo\t|\tDi\t|\tMi\t|\tDo\t|\tFr\t|\tSa\t|\tSo\t|\n-----------------------------------------------------------------------------------------------------\n"); 


     foreach (DateTime dateTime in dateList) 
     { 

//the part that doesnt work 
      int day = (int)dateTime.DayOfWeek; 

      if (dateTime == new DateTime(dateTime.Year, dateTime.Month, 1)) 
      { 
       for (day = 1; day == (int)dateTime.DayOfWeek; day++) 
       { 
        stringBuilder.Append("0"); 
        dayCounter++; 
       } 
      } 
//until here 
      else 
      { 
       stringBuilder.Append(dateTime.Day); 
       dayCounter++; 
      } 

      if (dateTime.Day == 28) 
      { 
       stringBuilder.Append(" |"); 
      } 
      else 
      { 
       stringBuilder.Append("\t|\t"); 
      } 

      if (dayCounter == 7) 
      { 
       stringBuilder.Append("\n"); 
       dayCounter = 0; 
      } 

     } 
     Console.Write(stringBuilder); 
     Console.ReadKey(); 
+0

私があなたに何をしようとしているのか、現在の結果が本当に明確ではないかと心配です。これを[mcve]に減らすことはできますか?内側のループだけが関連しているように見えますが、わかりませんが、具体的な例があれば助けがさらに簡単になります。 –

+0

残りのコードは、forループとif文以外のすべてを意味し、単にカレンダーシートを生成します。列にはタイトルとしてMo-Frがあり、その下にはWindowsのカレンダーと同様に日付が表示されます。私は日付を追加する方法は、それはいつも月曜日から始まり、私はその月の最初の実際の平日にそれをプッシュしたい。私は残りを取り除きます。 – tweedledum11

+0

問題を理解するために、外部foreachループが必要ですか?これを*完全な例にハードコード化された日付で変換し、コピー、ペースト、コンパイル、実行できるフルコンソールアプリケーションで変換できますか?問題の再現が簡単になると、簡単に手助けすることができます。 –

答えて

0

私はあなたのための小さなproblem.Howeverソリューションを作成し、それは

//This class will store the given month information 
class MonthInfo 
{ 
    public DayOfWeek DayName { get; set; } 
    public DateTime DayDate { get; set; } 
} 

//This class will store the cursor position, just to make it look like a calendar 
class CursorPosition 
{ 
    public string DayName { get; set; } 
    public DayOfWeek DayWeek { get; set; } 
    public int locationx { get; set; } 


} 

、ここれる

のStringBuilderを使用していませんコードの残りの部分

static void Main(string[] args) 
    { 
     int monthdigit = 0; 
     DateTime _date; 


     Console.WriteLine("Enter Month"); 
     string monthName= Console.ReadLine(); //should be in format "Jan","Feb" 


     if (DateTime.TryParseExact(monthName, "MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date)) 
     { 
      monthdigit = (int)_date.Month; 
     } 
     else 
     { 
      Console.WriteLine("invalid..programm will exit"); 
      return; 
     } 




     List<MonthInfo> GetMyDate = GetDates(2016, monthdigit); 


     //stores the cursor position to align the days accordingly 
     List<CursorPosition> CurorList = new List<CursorPosition>(); 
     CurorList.Add(new CursorPosition() { DayName = "Sun", DayWeek=DayOfWeek.Sunday }); 
     CurorList.Add(new CursorPosition() { DayName = "Mon", DayWeek = DayOfWeek.Monday }); 
     CurorList.Add(new CursorPosition() { DayName = "Tue", DayWeek = DayOfWeek.Tuesday }); 
     CurorList.Add(new CursorPosition() { DayName = "Wed", DayWeek = DayOfWeek.Wednesday }); 
     CurorList.Add(new CursorPosition() { DayName = "Thu", DayWeek = DayOfWeek.Thursday }); 
     CurorList.Add(new CursorPosition() { DayName = "Fri", DayWeek = DayOfWeek.Friday }); 
     CurorList.Add(new CursorPosition() { DayName = "Sat", DayWeek = DayOfWeek.Saturday }); 


     //print all the days name 
     foreach (CursorPosition _activeCursor in CurorList) 
     { 
       Console.Write("\t{0}",_activeCursor.DayName); 
       _activeCursor.locationx = Console.CursorLeft; 

     } 



     //retreive the cursor position and display your day index by adjusting the rownumber accordingly. 
     int _dayIndex = 1; 
     int rownumber = 2; 

     foreach (MonthInfo _month in GetMyDate) 
     { 
      Console.WriteLine(); 

       int positionx = (from p in CurorList 
           where p.DayWeek == _month.DayName 
           select p.locationx).Single(); 



      Console.SetCursorPosition(positionx, rownumber + 1); 
      Console.Write(_dayIndex++.ToString()); 



       if ((int)_month.DayName== 6) 
       { 
        rownumber++; 
        Console.WriteLine(); 
       } 



     } 

     Console.ReadKey(); 

    } 

    public static List<MonthInfo> GetDates(int year, int month) 
    { 
     var query=from date in Enumerable.Range(1, DateTime.DaysInMonth(year, month)) // Days: 1, 2 ... 31 etc. 
        select new MonthInfo() { DayName = new DateTime(year, month, date).DayOfWeek, DayDate = new DateTime(year, month, date) }; 

     return query.ToList(); 
    } 
+0

ありがとうございました。私のカレンダーは別の方法で動作します。使用するコードの一部がかなり進んでいるように見えるので、はるかに簡単です。私は3年間の学校生活を始めています。私たちが何を使い、どのようにコードを作成するのかを説明する必要があります。今、私のカレンダーは、ユーザーによって押されたキーを読むことによって動作します。つまり、次のことを意味します。RightArrow key =次の月に移動するなど。私はstringbuilderが理想的ではないかもしれないことは知っていますが、月の最初の日が見つかるとAppendingを開始する方法はありませんか? – tweedledum11