简体   繁体   English

在我的C#程序中未获得预期的输出,文件中的日期格式不正确

[英]Not getting the expected output in my c# program, Incorrect Format of Date is getting written in the file

I've written code that should not accept incorrect format of the date. 我编写了不应接受日期格式错误的代码。 Mine displays invalid choice but stores the incorrect format of the date in the text file. Mine显示无效的选择,但在文本文件中存储了错误的日期格式。 If the date is in incorrect format, it should not be stored in the text file. 如果日期格式不正确,则不应将其存储在文本文件中。

using System;
using System.Collections;
using System.IO;
using System.Globalization;
class FunWithScheduling
{
    public void AddView()
    {
        FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
        StreamWriter w = new StreamWriter(s);
        Console.WriteLine("Enter the Name of the Person To Be Met:");
        string Name = Console.ReadLine();
        Console.WriteLine("Enter the Date Scheduled For the Meeting:");
        string Date = Console.ReadLine();
        DateTime date;

        if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
        {
            Console.WriteLine("Invalid Choice");
        }
        Console.WriteLine("Enter the Time Scheduled For the Meeting:");
        string Time = Console.ReadLine();
        string line = Name + "                                "+ Date +"             " + Time;
        w.WriteLine(line);
        w.Flush();
        w.Close();
        s.Close();
    }

    static void Main()
    {
        FunWithScheduling a = new FunWithScheduling();
        a.AddView();
    }
} 

This modified program doesn't work. 此修改后的程序无法正常工作。 While is never ending and the correct format of the date is not accepted nor saved in the text file. While永远不会结束,并且日期的正确格式不会被接受,也不会保存在文本文件中。

I am not allowed to use the string builder. 我不允许使用字符串生成器。


using System;
using System.Collections;
using System.IO;
using System.Globalization;
class FunWithScheduling
{
    public void AddView()
    {
        FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
        StreamWriter w = new StreamWriter(s);
        Console.WriteLine("Enter the Name of the Person To Be Met:");
        string Name = Console.ReadLine();
        Console.WriteLine("Enter the Date Scheduled For the Meeting:");
        string Date = Console.ReadLine();
        DateTime date;
        if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
        {
            Console.WriteLine("Invalid date format!");
            while(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
            {
                Console.WriteLine("Invalid Date Entered, please format MM-dd-yyyy");
                Date = Console.ReadLine();
            }
        }
        Console.WriteLine("Enter the Time Scheduled For the Meeting:");
        string Time = Console.ReadLine();
        string line = Name + "                                "+ Date +"             " + Time;
        w.WriteLine(line);
        w.Flush();
        w.Close();
        s.Close();
    }

    static void Main()
    {
        FunWithScheduling a = new FunWithScheduling();
        a.AddView();
    }
}

You have to add: 您必须添加:

return;

after : 之后:

Console.WriteLine("Invalid Choice");

Also better to move FileStream and StreamWriter initialization till after the condition check: FileStreamStreamWriter初始化移动到条件检查之后也更好:

using System;
using System.Collections;
using System.IO;
using System.Globalization;
class FunWithScheduling
{
     public void AddView()
     {
        Console.WriteLine("Enter the Name of the Person To Be Met:");
        string Name = Console.ReadLine();
        Console.WriteLine("Enter the Date Scheduled For the Meeting:");
        string Date = Console.ReadLine();
        DateTime date;
        if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
        {
               Console.WriteLine("Invalid Choice");
               return;
        }
        Console.WriteLine("Enter the Time Scheduled For the Meeting:");
        string Time = Console.ReadLine();
        string line = Name + "                                "+ Date +"             " + Time;
        FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
        StreamWriter w = new StreamWriter(s);
        w.WriteLine(line);
        w.Flush();
        w.Close();
        s.Close();
      }
      static void Main()
      {
        FunWithScheduling a = new FunWithScheduling();
        a.AddView();
      }
} 

I suggest you use an "else" statement to fix issue of "Date". 我建议您使用“其他”语句来解决“日期”问题。 And then use a String Builder instead of + and build the string. 然后使用String Builder而不是+构建字符串。

I've tried to help you here, but I haven't run it through a compiler. 我在这里曾尝试为您提供帮助,但尚未通过编译器运行它。

using System;
using System.Collections;
using System.IO;
using System.Globalization;
using System.Text;
class FunWithScheduling
{
     public void AddView()
     {
        FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
        StreamWriter w = new StreamWriter(s);
        var builder = new StringBuilder();
        Console.WriteLine("Enter the Name of the Person To Be Met:");
        string Name = Console.ReadLine();
        builder.Append(Name);
        Console.WriteLine("Enter the Date Scheduled For the Meeting:");
        string Date = Console.ReadLine();
        DateTime date;
        if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
        {
               Console.WriteLine("Invalid Choice");
        } else {
            builder.Append(date.ToString("MMMM dd, yyyy"));
        }
        Console.WriteLine("Enter the Time Scheduled For the Meeting:");
        string Time = Console.ReadLine();
        builder.Append(Time);
        w.WriteLine(builder.ToString());
        w.Flush();
        w.Close();
        s.Close();
      }
      static void Main()
      {
        FunWithScheduling a = new FunWithScheduling();
        a.AddView();
      }
} 

You could also try a while statement similar to this in order to force the user to input the right date. 您也可以尝试类似的while语句,以强制用户输入正确的日期。

    while(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date)){
        Console.WriteLine("Invalid Date Entered, please format MM-dd-yyyy");
        Date = Console.ReadLine();
    }

// Make Date as blank when entered date is invalid; //如果输入的日期无效,则将日期设为空白; it will not be stored in the text file. 它不会存储在文本文件中。

if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",
   new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
    Console.WriteLine("Invalid Choice");
    Date = "";
}

First of all, yes, use a StringBuilder instead of just + with string, because when you do that, string will multiply the memory usage of both of the strings, which means you have 2 memory allocations for 2 separated strings, and 1 for the con-catted one, just a tip for future. 首先,是的,请使用StringBuilder而不是仅将+与string配合使用,因为这样做时,string会将两个字符串的内存使用量相乘,这意味着您为2个单独的字符串分配了2个内存分配,而对于强调,只是未来的秘诀。

All you need to do is either do 您需要做的就是

StringBuilder sb = new StringBuilder();

sb.Append(Name);

if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
    {
           Console.WriteLine("Invalid Choice");

    }
    else 
    {
        sb.Append(Date);
    }

and then when you write to the file itself just do 然后当您写入文件本身时

w.WriteLine(sb.ToString());

You have 2 options when the user enter the wrong date: 用户输入错误的日期时,您有2个选项:

  1. Close the stream and exit out of the code. 关闭流并退出代码。
  2. Ask the user to re-enter the date by specifying the format. 通过指定格式要求用户重新输入日期。

To close the stream and exit out you must add below code after: 要关闭流并退出,必须在以下位置添加以下代码:

Console.WriteLine("Invalid Choice");
w.Flush();
w.Close();
s.Close();
return;

And

To ask the user to re-enter, until he specifies the correct answer, add below code after: 要要求用户重新输入,直到他指定正确的答案,然后在下面添加以下代码:

Console.WriteLine("Invalid Choice");

while(!DateTime.TryParseExact(Date, "MM-dd-yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out date))
{    
    Console.WriteLine("Invalid Date Entered, please format MM-dd-yyyy");
    Date = Console.ReadLine();
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM