C#正在获取用户输入的第二天

本文关键字:输入 第二天 用户 获取 | 更新日期: 2023-09-27 18:21:41

我正在创建自己的日期类,该类应获得用户输入的第二天。例如,如果用户键入18/03/1920,则应在第二天返回。当前的格式为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/yy");
            Console.ReadLine();
            date i = new date(01, 12, 1920);
            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
            private int value;
            public int year
            {
                get;
                private set;
            }
            public date(int day, int month, int year)
            {
                this.day = day;
                this.month = month;
                this.year = year;
                Console.WriteLine("Date object constructor for date {0}");
            }
            public int getYear()
            {
                return year;
            }
            public void setYear()
            {
                if (value > 1900 && value <= 2020)
                    year = value;
                else
                    throw new ArgumentOutOfRangeException("year", value, "out of bounds");
            }
            public int getMonth()
            {
                return month;
            }
            public void setMonth()
            {
                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("Day", value, "day is out of range");
            }
        }
    }
}

C#正在获取用户输入的第二天

看起来您混淆了属性和方法。

这里value是一个未知变量。这将导致Cannot resolve symbol错误。我确信它和Java中的一样——你绝对不能使用不存在的变量:)

public void setMonth()
{
    if (value > 0 && value <= 12)
        month = value; // <-- value is unknown
    else
        throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12");
}

同时,在C#中,我们有一些属性。它们允许您方便地封装访问''设置逻辑:

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

然后,您将能够使用它:

var dt = new date(1, 1, 1);
dt.Month = 5; // property setter
Console.WriteLine(dt.Month); // property getter
dt.Month = 17; // <-- throws exception

只需将setSomethinggetSomething方法转换为属性,就会发现它是有效的。

请注意,如果变量具有属性,则不应将其公开。

public int month; // shouldn't be public
private int Month 
{ 
    get { return month; }
    set 
    {
        if (value > 0 && value <= 12)
            month = value; // <-- here, value does exist
        else
            throw new ArgumentOutOfRangeException("Month", value, "Month must be 1-12");    
    }
}

所以,这个属性将变得无用,你将能够做到:

dt.Month = 17; // property setter throws exception
dt.month = 17; // simply sets field value

您需要隐藏此变量。

此外,正如我在评论中提到的,为了不混淆任何其他开发人员,请遵循C#命名约定。方法和类名应以大写字母开头。