2016-11-29 71 views
-7

我只需要一些指導我如何顯示當月秋季當天的日期。例如,如果用戶在當月的星期一和31天輸入0,則它將顯示所有星期幾和當月星期幾對應的日期?感謝我迄今爲止的這一點,但它似乎不適用於用戶輸入。C#二維數組日曆日月

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Calendar 
{ 
    class Program 
    { 
     static int startDay = new int(); 
     static int totalDays = new int(); 

     static void Main(string[] args) 
     { 
      Console.Write("Enter the day the first falls on (Mon = 0, etc): "); 
      startDay = Convert.ToInt32(Console.ReadLine()); 
      Console.Write("How many days in the month? "); 
      totalDays = Convert.ToInt32(Console.ReadLine()); 
     } 

    public static List<DateTime> getDates() 
    { 
     List<DateTime> lstSundays = new List<DateTime>(); 
     int intMonth = DateTime.Now.Month; 
     int intYear = DateTime.Now.Year; 
     int intDaysThisMonth = DateTime.DaysInMonth(intYear, intMonth); 
     DateTime oBeginnngOfThisMonth = new DateTime(intYear, intMonth, 1); 
     for (int i = 1; i < intDaysThisMonth + 1; i++) 
     { 
      if (oBeginnngOfThisMonth.AddDays(i).DayOfWeek == DayOfWeek.Sunday) 
      { 
       lstSundays.Add(new DateTime(intYear, intMonth, i)); 
      } 
     } 
     return lstSundays; 
    } 
} 

}

+0

歡迎來到Stack Overflow。請將代碼*作爲文本發佈*而不是圖片,並且您的要求太模糊,目前我們無法幫助您。請閱讀https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/並記住Stack Overflow的目標是創建一個高質量問題和答案的存儲庫。 –

+3

嗯...我不認爲創建一個新的配置文件並再次提出相同的問題,首先向您指出的信息缺乏完全相同的信息,將會有所幫助。 http://stackoverflow.com/questions/40869698/c-sharp-2d-arrays-calendar-month –

+0

我正要發佈相同的東西。將此添加到您的問題'該計劃應該要求您在一個月中的第一天(0到6之間)以及該月有多少天的一週中的某一天。它也應該要求這個月的名字。「你如何期待幫助?人們不能讀你的想法 –

回答

1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace calenderMonth1 
{ 
    class Program 
    { 
    int[][] months; 
    int day; 

    static void Main(string[] args) 
    { 
     Program calendar = new Program(); 
     calendar.Months(); 
     calendar.ColRow(); 
     calender.DisplayMonth(); 
    } 

    public void Display(int itr) 
    { 
     for (int i = 0; i < itr; i ++) 
     { 
      Console.WriteLine(); 
     } 
    } 

    public enum MonthNames 
    { 
     JANUARY, 
     FEBRUARY, 
     MARCH, 
     APRIL, 
     MAY, 
     JUNE, 
     JULY, 
     AUGUST, 
     SEPTEMBER, 
     OCTOBER, 
     NOVEMBER, 
     DECEMBER 
    } 
    public void Months() 
    { 
     int month; 
     int[] months = new int[12]; 
     for (int i = 0; i < months.Count(); i ++) 
     { 
      month = months[i]; 
      Console.WriteLine(month + "\t"); 
     } 
    } 

    public void ColRow() 
    { 
     months = new int[12][]; 
     for (int i = 0; i < 12; i ++) 
     { 
      switch (i) 
      { 
       case 0: 
        months[i] = new int[31]; 
        break; 
       case 1: 
        months[i] = new int[28]; //This may not always be true... Leap Year every 4 years 
        break; 
       case 2: 
        months[i] = new int[31]; 
        break; 
       case 3: 
        months[i] = new int[30]; 
        break; 
       case 4: 
        months[i] = new int[31]; 
        break; 
       case 5: 
        months[i] = new int[30]; 
        break; 
       case 6: 
        months[i] = new int[31]; 
        break; 
       case 7: 
        months[i] = new int[31]; 
        break; 
       case 8: 
        months[i] = new int[30]; 
        break; 
       case 9: 
        months[i] = new int[31]; 
        break; 
       case 10: 
        months[i] = new int[30]; 
        break; 
       case 11: 
        months[i] = new int[31]; 
        break; 
       default: 
        break; 
      } 

     } 

     int dayInYear = 1; 
     for (int thisMonth = 0; thisMonth < months.Count(); thisMonth++) 
     { 
      for (day = 0; day < months[thisMonth].Count(); day++) 
      { 
       months[thisMonth][day] = dayInYear; 
       dayInYear++; 
      } 

     } 
     for (int row = 0; row < 12; row ++) 
     { 
      for (int col =0; col < months[row].Count(); col ++)  //Make sure the array doesn't go out of bounds... 
      { 
       switch (row) 
       { 
        case 0: 
         Console.WriteLine(MonthNames.JANUARY.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 1: 
         Console.WriteLine(MonthNames.FEBRUARY.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 2: 
         Console.WriteLine(MonthNames.MARCH.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 3: 
         Console.WriteLine(MonthNames.APRIL.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 4: 
         Console.WriteLine(MonthNames.MAY.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 5: 
         Console.WriteLine(MonthNames.JUNE.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 6: 
         Console.WriteLine(MonthNames.JULY.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 7: 
         Console.WriteLine(MonthNames.AUGUST.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 8: 
         Console.WriteLine(MonthNames.SEPTEMBER.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 9: 
         Console.WriteLine(MonthNames.OCTOBER.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 10: 
         Console.WriteLine(MonthNames.NOVEMBER.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        case 11: 
         Console.WriteLine(MonthNames.DECEMBER.ToString() + "\t" + months[row][col] + "\n"); 
         break; 
        default: 
         break; 
       } 
      } 
     } 

    } 
} 

void DisplayMonth() 
{ 
     Console.WriteLine("Please insert the numerical value of the month you wish to display:\t"); 
     int myMonth = Convert.ToInt32(Console.ReadLine()); 
     myMonth -= 1; 
     Console.WriteLine(); 

     switch (myMonth) 
     { 
      case 0: 
       for (int col = 0; col < months[myMonth].Count(); col++) 
        Console.WriteLine(MonthNames.JANUARY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 1: 
       for (int col = 0; col < months[myMonth].Count(); col++) 
        Console.WriteLine(MonthNames.FEBRUARY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 2: 
       for (int col = 0; col < months[myMonth].Count(); col++) 
        Console.WriteLine(MonthNames.MARCH.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 3: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.APRIL.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 4: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.MAY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 5: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.JUNE.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 6: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.JULY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 7: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.AUGUST.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 8: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.SEPTEMBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 9: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.OCTOBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 10: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.NOVEMBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 11: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.DECEMBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      default: 
       break; 
     } 
    } 
} 
+0

非常感謝,我很感激 –

+0

這不顯示一個月的一天 - 而是一年中的哪一天它是..如果你想顯示一個月中的某一天,請切換爲: '// int dayInYear = 1;' 'for(int thisMonth = 0; thisMonth

+0

選中該複選框作爲答案,並在答案上投票 –

1
void DisplayMonth() 
{ 
    Console.WriteLine("Please insert the numerical value of the month you wish to display:\t"); 
    int myMonth = Convert.ToInt32(Console.ReadLine()); 
    myMonth -= 1; 
    Console.WriteLine(); 

    switch (myMonth) 
    { 
      case 0: 
       for (int col = 0; col < months[myMonth].Count(); col++) 
        Console.WriteLine(MonthNames.JANUARY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 1: 
       for (int col = 0; col < months[myMonth].Count(); col++) 
        Console.WriteLine(MonthNames.FEBRUARY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 2: 
       for (int col = 0; col < months[myMonth].Count(); col++) 
        Console.WriteLine(MonthNames.MARCH.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 3: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.APRIL.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 4: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.MAY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 5: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.JUNE.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 6: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.JULY.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 7: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.AUGUST.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 8: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.SEPTEMBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 9: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.OCTOBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 10: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.NOVEMBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      case 11: 
       for (int col = 0; col < months[myMonth].Count()-1; col++) 
        Console.WriteLine(MonthNames.DECEMBER.ToString() + "\t" + months[myMonth][col] + "\n"); 
       break; 
      default: 
       break; 
     } 
    } 

取現場周圍看看。獲取你的方位。任何人都很難在沒有看到你的代碼的情況下幫助你,而且一張照片無法幫助你。你可以把它放在你的問題中,當你縮進時它會以代碼形式出現。你也可以把圖像放在你的問題中 - 不必是(也不應該是)圖像鏈接。讓自己更容易獲得答案,讓我們更容易爲您提供答案。

+0

另外,請查看https://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx –

+0

你是在殺我小。 –

+0

即時通訊抱歉,但目前它只是太長,我不知道要刪除或重新刪除哪些位 –