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

时间:2013-08-05 05:29:29

标签: c#

我编写的代码不应该接受错误的日期格式。 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();
    }
} 

此修改后的程序不起作用。虽然永远不会结束,但不接受日期的正确格式,也不保存在文本文件中。

我不允许使用字符串构建器。


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

5 个答案:

答案 0 :(得分:3)

你必须添加:

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

答案 1 :(得分:0)

我建议您使用“else”语句来解决“Date”问题。然后使用String Builder代替+并构建字符串。

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

首先,是的,使用StringBuilder而不是+ with string,因为当你这样做时,string将乘以两个字符串的内存使用量,这意味着你有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());

答案 4 :(得分:0)

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

  1. 关闭流并退出代码。
  2. 要求用户通过指定格式重新输入日期。
  3. 要关闭流并退出,您必须在以下代码后添加以下代码:

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