0

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

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

5 回答 5

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();
      }
} 
于 2013-08-05T05:34:09.123 回答
0

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

if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",
   new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
    Console.WriteLine("Invalid Choice");
    Date = "";
}
于 2013-08-05T05:54:33.250 回答
0

首先,是的,使用 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());
于 2013-08-05T05:55:29.803 回答
0

我建议您使用“else”语句来解决“日期”问题。然后使用字符串生成器代替+并构建字符串。

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

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();
    }
于 2013-08-05T05:34:07.030 回答
0

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

  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();
}    
于 2013-08-05T05:57:01.203 回答