2015-11-19 69 views
-3

我已經完成了一個名爲nextDay的方法,它應該增加用戶在控制檯中鍵入日期的那一天。該方法還根據輸入的內容更改年份並更改月份。例如。如果我在我的控制檯中輸入13/10/2012我希望輸出是14/10/2012,我希望根據幾個月和一年。任何方式,我可以實現這一點或修復我的當前方法,以便此輸出工作?目前我的程序工作正常,只檢索用戶輸入的日期,並且還檢查錯誤。我特別要求的是在用戶輸入之前的第二天。C#增量天數

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("\t\t\t\t\t\tNextDate Application\n\t\t\t\t\t-------------------------------------"); // title 

      Console.WriteLine("This application will allow you to enter a valid date and this will get the next day.\n"); // intro of what the application does 

      Console.WriteLine("please enter date as dd/MM/yyyy\n"); // tells user to input a date in the formats 
      int day; // sets variable 
      int month; // sets variable 
      int year; // sets variable 

      string[] read = Console.ReadLine().Split('/'); // "/" can be read from each value and sets new array 
      day = int.Parse(read[0]); // day is first position in array 
      month = int.Parse(read[1]); // month is second position in array 
      year = int.Parse(read[2]); // year is third position in array 

      try 
      { 
       Date date = new Date(day, month, year); // initialises a new date class 
       Console.WriteLine("{0}/{1}/{2}", date.Day, date.Month, date.Year); // given to user as "day/month/year" 
       Console.ReadLine(); // reads the line 
      } 
      catch (ArgumentOutOfRangeException exc) 
      { 
       Console.WriteLine(exc.Message); // states the message for ArgumentOutOfRangeException 
       Console.Read(); // breaks 
      }  
     } 
     class Date 
     { 
      private int _month; // 1-12 
      private int _day; // 1-31 depending on month 
      private int _year; // sets the year 

      public Date(int day, int month, int year) 
      { 
       Month = month; 
       Day = day;   
       Year = year; 
      } 

      public void nextDay() 
      { 
       try 
       { 
        _day = _day++; 
       } 
       catch (ArgumentOutOfRangeException) 
       { 
        if (_month == 12) // e.g if month dec 31st then it does a try 
        { 
         _month = 1; // month then = 1 
         _year++; // year increments 
        } 

        else 
        { 
         _month++; 
        } 
        _day = 1; 
       } 

      } 

      public int Year 
      { 
       get { return _year; } 
       set 
       { 
        if (value >= 1820 && value <= 2020) // if value is higher than or equal to 1820 and less than or equal to 2020 
         _year = value; // sets year as value 
        else 
         throw new ArgumentOutOfRangeException("Year must be between 1820 and 2020"); // throws an exception 
       } 
      } 

      public int Month 
      { 
       get { return _month; } 
       set 
       { 
        if (value > 0 && value <= 12) // if value is higher than 0 and less than or equal to 12 
         _month = value; // sets month as value 
        else 
         throw new ArgumentOutOfRangeException("Month must be between 1-12"); // throws an exception 
       } 
      } 

      public int Day 
      { 
       get { return _day; } 
       set 
       { 
        int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // array of days of max each month 

        if (value > 0 && value <= days[_month]) // if value is higher than 0 and less than or equal to days of month 
         _day = value; // sets day as value 

        else if (_month == 2 && value == 29 && // else if month is equal to 2 and value is equal to 29 
         _year % 400 == 0 || (_year % 4 == 0 && _year % 100 != 0)) 
         _day = value; 
        else 
         throw new ArgumentOutOfRangeException("Day is out of range"); // throws an exception 
       } 
      } 
     } 
    } 
} 
+3

有,爲什麼你想實現自己的日期處理的理由? DateTime讓你失望,還是這是一項學術演習? – spender

+0

您忘記告訴我們您當前的實施*如何不按預期工作。有錯誤嗎?意外的輸出?當你調試這個,特別是它失敗?發生這種情況時的運行時間值是什麼? – David

+0

日期工作正常,但是當我輸入日期時,我希望第二天。這是爲了實驗的目的,我想玩弄c# – ramteen1993

回答

0

務實的答案:

public Date AddDay() 
{ 
    var tomorrow = new DateTime(this._year, this._month, this._year).AddDays(1).Date; 
    return new Date(tomorrow.Day, tomorrow.Month, tomorrow.Year); 
} 

或者如果你喜歡突變

public void AddDay() 
{ 
    var tomorrow = new DateTime(this._year, this._month, this._day).AddDays(1).Date; 
    this._day = tomorrow.Day; 
    //etc... 
} 
-1

試試這個:

private static void Main(string[] args) 
    { 
     DateTime theDateTime = DateTime.Now; 
     DateTime tomorrow = NextDay(theDateTime); 
     Console.WriteLine($"Now: {theDateTime:yy/MM/dd}\t\tTomorrow:" + 
      $"{tomorrow:yy/MM/dd}"); 
    } 

    private static DateTime NextDay(DateTime source) 
    { 
     return source + new TimeSpan(1, 0, 0, 0); 
    } 

需要適應你的代碼。解析輸入日期時間。使用方法。相應地解析輸出。

+0

c#6.0 touch ..哈哈哈好試試 –

+0

向下投票關心評論? –

-1

你的問題是你的所有邏輯都在你的屬性中,但是在你的nextDay方法中,你正在增加本地字段。最重要的是,以下內容不會導致任何變化。

_day = _day++; 

,因爲這相當於

int temp = _day; 
_day = _day + 1; 
_day = temp;