在我的c#程序中没有得到预期的输出,文件中写入了错误的日期格式

本文关键字:文件 输出 格式 日期 错误 程序 我的 | 更新日期: 2023-09-27 18:26:19

我编写的代码不应该接受错误的日期格式。Mine显示无效的选择,但在文本文件中存储了不正确的日期格式。如果日期格式不正确,则不应将其存储在文本文件中。

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();
    }
} 

这个修改后的程序不起作用。While永远不会结束,并且不会接受正确的日期格式,也不会保存在文本文件中。

不允许我使用字符串生成器。


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();
    }
}

在我的c#程序中没有得到预期的输出,文件中写入了错误的日期格式

您必须添加:

return;

之后:

Console.WriteLine("Invalid Choice");

同样最好将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();
      }
} 

我建议您使用"else"语句来解决"Date"的问题。然后使用字符串生成器而不是+来构建字符串。

我试图在这里帮助你,但我还没有通过编译器运行它。

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();
      }
} 

您也可以尝试类似的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();
    }

//当输入的日期无效时,将日期设为空;它将不会存储在文本文件中。

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

首先,是的,使用StringBuilder,而不是只使用字符串+,因为当你这样做时,字符串将乘以两个字符串的内存使用量,这意味着你有2个内存分配给2个分离的字符串,1个内存分配用于合并的字符串,这只是未来的一个提示。

你所需要做的就是要么做

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

然后当你向文件本身写入时,只需进行

w.WriteLine(sb.ToString());

当用户输入错误的日期时,您有两个选项:

  1. 关闭流并退出代码
  2. 要求用户通过指定格式重新输入日期

要关闭流并退出,您必须在之后添加以下代码:

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

要要求用户重新输入,直到他指定了正确的答案,请在以下代码后添加:

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();
}