Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有stringbuilder日历表的控制台日历_C#_Calendar_Console_Stringbuilder - Fatal编程技术网

C# 带有stringbuilder日历表的控制台日历

C# 带有stringbuilder日历表的控制台日历,c#,calendar,console,stringbuilder,C#,Calendar,Console,Stringbuilder,我正在制作一个基于控制台的日历,作为一项教育任务(学校)。它需要显示用户输入的特定月份的日历表。我快到了,但我注意到现在所有的月份都从周一开始。我用一个stringbuilder创建了这个工作表,并且我尝试用零填充月的第一天之前的几天,但是它还不起作用。for循环是我的尝试。有人有什么想法吗?我还在学习。下面是构建工作表的代码,除了这个问题外,该代码运行良好: DateTime date = DateTime.Now; List<DateTime> dateList

我正在制作一个基于控制台的日历,作为一项教育任务(学校)。它需要显示用户输入的特定月份的日历表。我快到了,但我注意到现在所有的月份都从周一开始。我用一个stringbuilder创建了这个工作表,并且我尝试用零填充月的第一天之前的几天,但是它还不起作用。for循环是我的尝试。有人有什么想法吗?我还在学习。下面是构建工作表的代码,除了这个问题外,该代码运行良好:

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();
DateTime date=DateTime.Now;
列表日期列表=新列表();
DateTime dateCopyForModification=日期;
int inputMonth=日期.月份;
while(DateCopyFormModification.Month==inputMonth)
{
dateList.Add(dateCopyForModification);
DateCopyFormModification=DateCopyFormModification.AddDays(1);
}
Console.WriteLine(“\t-------------\n\t{0},{1}\n\t----------”,date.ToString(“MMMM”),date.Year);
int dayCounter=0;
StringBuilder StringBuilder=新的StringBuilder();
stringBuilder.Append(“\nMo\t | \tDi\t | \tMi\t | \tDo\t | \tFr\t | \tSa\t | \tSo\t | \n--------------------------------------------------------------------------------------------------------------------------\n”);
foreach(日期列表中的日期时间DateTime)
{
//不起作用的部分
int day=(int)dateTime.DayOfWeek;
如果(dateTime==新的dateTime(dateTime.Year,dateTime.Month,1))
{
对于(day=1;day==(int)dateTime.DayOfWeek;day++)
{
stringBuilder.Append(“0”);
dayCounter++;
}
}
//直到这里
其他的
{
stringBuilder.Append(dateTime.Day);
dayCounter++;
}
如果(dateTime.Day==28)
{
stringBuilder.Append(“|”);
}
其他的
{
stringBuilder.Append(“\t |\t”);
}
如果(dayCounter==7)
{
stringBuilder.Append(“\n”);
dayCounter=0;
}
}
Console.Write(stringBuilder);
Console.ReadKey();

我为您的问题创建了一个小型解决方案。但是它不使用stringbuilder

//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; }


}
下面是代码的其余部分

 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();
    }
static void Main(字符串[]args)
{
int-monthdigit=0;
日期时间(DateTime)日期;;
Console.WriteLine(“输入月份”);
字符串monthName=Console.ReadLine();//格式应为“Jan”、“Feb”
if(DateTime.TryParseExact(monthName,“MMM”,CultureInfo.InvariantCulture,datetimestyle.None,out\u date))
{
monthdigit=(int)\u date.Month;
}
其他的
{
Console.WriteLine(“无效..程序将退出”);
返回;
}
列表GetMyDate=GetDates(2016年,月数位);
//存储光标位置以相应地对齐日期
List CurorList=新列表();
Add(new CursorPosition(){DayName=“Sun”,DayWeek=DayOfWeek.Sunday});
添加(new CursorPosition(){DayName=“Mon”,DayWeek=DayOfWeek.Monday});
Add(new CursorPosition(){DayName=“Tue”,DayWeek=DayOfWeek.周二});
添加(new CursorPosition(){DayName=“Wed”,DayWeek=DayOfWeek.周三});
添加(new CursorPosition(){DayName=“Thu”,DayWeek=DayOfWeek.星期四});
添加(new CursorPosition(){DayName=“Fri”,DayWeek=DayOfWeek.Friday});
添加(new CursorPosition(){DayName=“Sat”,DayWeek=DayOfWeek.Saturday});
//打印所有日期名称
foreach(CursorPosition\u CurorList中的activeCursor)
{
Write(“\t{0}”,_activeCursor.DayName);
_activeCursor.locationx=Console.CursorLeft;
}
//检索光标位置,并通过相应地调整行数来显示日索引。
int_dayIndex=1;
int rownumber=2;
foreach(GetMyDate中的月)
{
Console.WriteLine();
int positionx=(从CurorList中的p开始
其中p.DayWeek==\u month.DayName
选择p.locationx).Single();
控制台。设置光标位置(位置X,行编号+1);
Console.Write(_dayIndex++.ToString());
如果((int)_month.DayName==6)
{
行数++;
Console.WriteLine();
}
}
Console.ReadKey();
}
公共静态列表GetDates(整数年,整数月)
{
var query=Enumerable.Range中的from date(1,DateTime.DaysInMonth(year,month))//天:1,2…31等。
选择new MonthInfo(){DayName=new DateTime(年、月、日)。DayOfWeek,DayDate=new DateTime(年、月、日)};
返回query.ToList();
}

我恐怕不太清楚您想做什么,或者当前的结果是什么。你能把这个简化成一个简单的问题吗?看起来只有内部循环是相关的,但我们不能确定-一个具体的例子会使它更容易帮助您。代码的其余部分,即除了内部for循环和if语句之外的所有内容,只生成一个日历表。这些列的标题是Mo Fr,下面是日期,就像Windows日历一样。不过,按照我附加日期的方式,它总是从周一开始,我想把它推到实际的工作日,也就是一个月的第一天。我将删除其余的。为了理解这个问题,我们需要外部foreach循环吗?你能把它转换成一个完整的例子,用硬编码的日期,在一个完整的控制台应用程序中,我们可以复制,粘贴,编译和运行吗?你越简单,我们就越容易复制