2015-11-13 46 views
0

我正在尋找解決方案,以獲取如何在一行中獲取我所有的int解析。在我開始我的課程的那一刻,我必須輸入日,月和年。這一切都是逐行完成的。我想要一個解決方案或方法,它可以在一行內以「dd/MM/yyyy」格式完成所有解析。如何解析一行?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace date 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("please enter date as dd/MM/yyyy"); 

      int day; 
      int month; 
      int year; 


      day = int.Parse(Console.ReadLine()); 
      month = int.Parse(Console.ReadLine()); 
      year = int.Parse(Console.ReadLine()); 
      Date i = new Date(day, month, year); 



      Console.WriteLine("{0}/{1}/{2}", i.day, i.month, i.year); 
      Console.ReadLine(); 
     } 

     class Date 
     { 
      public int month; // 1-12 
      public int day; // 1-31 depending on month 
      int value = 1; 

      public int year 
      { 
       get; 
       private set; 
      } 


      public Date(int day, int month, int year) 
      { 
       this.day = day; 
       this.month = month; 
       this.year = year; 
      } 

      public int GetYear() 
      { 
       return year; 
      } 

      public void SetYear() 
      { 
       if (value > 1900 && value <= 2020) 
        year = value; 
       else 
        throw new ArgumentOutOfRangeException("year", value, "out of bounds"); 
      } 

      private int Month 
      { 
       get { return month; } 
       set 
       { 
        if (value > 0 && value <= 12) 
         month = value; 
        else 
         throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12"); 
       } 
      } 

      public int GetDay() 
      { 
       return day; 
      } 

      public void SetDay() 
      { 
       int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 

       if (value > 0 && value <= days[month]) 
        day = value; 

       else if (month == 2 && value == 29 && 
        year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) 
        day = value; 
       else 
        throw new ArgumentOutOfRangeException("days", value, "day is out of range"); 
      } 

     } 

    } 

} 
+3

爲什麼在已經有內置類型'DateTime'的時候爲'Date'編寫了一個類,該類具有類的所有功能。這看起來像一個經典的「不要重新發明輪子」的情況。 –

+0

@ roryap這是我想要做的練習。 –

+0

您告訴用戶輸入日期爲「dd/MM/yyyy」,但是隨後使用三個「ReadLine()」調用......這是行不通的! – crashmstr

回答

0

不能解析dd/MM/yyyy格式化字符串爲int,因爲它是不是有效的字符串。

您需要首先解析它到DateTime,然後您可以使用它的屬性將它的日,月和年作爲數字。

例如;

Console.WriteLine("please enter date as dd/MM/yyyy"); 
DateTime dt = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", 
            CultureInfo.InvariantCulture); 

int day = dt.Day; 
int month = dt.Month; 
int year = dt.Year; 

或者你可以使用TryParseExact,如果你輸入不dd/MM/yyyy格式不想拋出異常。

我不想使用預設DateTime類,但我用我的 自己的「日期」類

如果是這樣,你解析後,您可以創建Date(int day, int month, int year)Date類實例基於此dt值的構造函數爲;

Date myDt = new Date(dt.Day, dt.Month, dt.Year); 
+0

我不想使用預設的DateTime類,但我使用我自己的「Date」類。 –

+0

@MinalModi更新了我的答案。看一看。 –

1

您可以使用DateTime.Parse/TryParseDateTime.ParseExact/TryParseExact

string line = Console.ReadLine(); 
DateTime dt; 
bool validDate = DateTime.TryParseExact(line,"dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo,DateTimeStyles.None, out dt); 
if(validDate) 
    Console.WriteLine(dt.ToLongDateString()); // now correctly initialized 

採用這種格式也DateTime.Parse/DateTime.TryParse作品:

validDate = DateTime.TryParse(line, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt); 

我用DateTimeFormatInfo.InvariantInfo,以防止當地日期分隔符代替/ (如果不同)。

一旦它被解析爲DateTime是微不足道的創建Date實例:

Date d = new Date(dt.Day, dt.Month, dt.Year); 
+0

我不想使用預設的DateTime類,但我使用自己的「Date」類。 –

+0

@MinalModi:我編輯了我的答案。 DateTime具有你所需要的所有功能,DateTime.Day,DateTime.Month和DateTime.Year。 –

0

如果你不想使用日期時間(嘗試)解析,您可以用正則表達式添加parse方法你日期類別:

public static Date ParseFromString(string s) 
    { 
    //string s = "24/12/2015"; 
    Regex r = new Regex(@"(\d+)[\/](\d+)[\/](\d+)"); 
    Match m = r.Match(s); 
    if (m.Success) 
    { 
     return new Date(m.Groups[1], m.Groups[2], m.Groups[3]); 
    } 
    else 
    { 
     // throw exception 
    } 
    }