
C# 日期 构造 5
要求用面向对象方法,编写日期类RiQi,包含年、月、日等字段,要求用int类型构造方法,要求全参并验证参数是否合法,包括年不能为负数,月在1到12范围内,日根据月份确定范...
要求用面向对象方法,编写日期类RiQi,包含
年、月、日等字段,要求用int类型
构造方法,要求全参并验证参数是否合法,包括年不能为负数,月在1到12范围内,日根据月份确定范围,分别为:1、3、5、7、8、10、12月为31天,2月为28天,其他月份为30天。
相应的属性的实现
根据月、日计算天数的方法(即计算该月日为这年的第几天)
编写主程序class OneTest(统一使用主类名称:OneTest),包含
从键盘接受输入的方法,要求用string 类型返回
验证输入内容是否符合规定格式的方法,要求如果不符合格式,提示信息,并返回null,,如果符合格式,将输入内容分成年、月、日三部分返回(提示:输入规定格式为:2006-7-28,参数类型为string,返回类型为string 数组)
将string 类型转换为 int 的方法
main方法
新手做不出,调了。好久都不行。
help 展开
年、月、日等字段,要求用int类型
构造方法,要求全参并验证参数是否合法,包括年不能为负数,月在1到12范围内,日根据月份确定范围,分别为:1、3、5、7、8、10、12月为31天,2月为28天,其他月份为30天。
相应的属性的实现
根据月、日计算天数的方法(即计算该月日为这年的第几天)
编写主程序class OneTest(统一使用主类名称:OneTest),包含
从键盘接受输入的方法,要求用string 类型返回
验证输入内容是否符合规定格式的方法,要求如果不符合格式,提示信息,并返回null,,如果符合格式,将输入内容分成年、月、日三部分返回(提示:输入规定格式为:2006-7-28,参数类型为string,返回类型为string 数组)
将string 类型转换为 int 的方法
main方法
新手做不出,调了。好久都不行。
help 展开
展开全部
class RiQi
{
int year;
int month;
int day;
public RiQi(int y, int m, int d)
{
if (y > 0)
{
this.year = y;
}
this.month = m;
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
this.day = 31;
break;
case 2:
this.day = 28;
break;
case 4:
case 6:
case 9:
case 11:
this.day = 30;
break;
default:
this.month = 0;
Console.WriteLine("非法");
}
}
public int cal()
{
//实现计算的过程
}
}
//主函数的验证代码
if(input.IndexOf("-")!=-1)
{
string[] str = input.Split("-");
if (str == 3)
{
try
{
int y = int.Parse(str[0]);
}
catch (Exception ex)
{
}
}
}
}
{
int year;
int month;
int day;
public RiQi(int y, int m, int d)
{
if (y > 0)
{
this.year = y;
}
this.month = m;
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
this.day = 31;
break;
case 2:
this.day = 28;
break;
case 4:
case 6:
case 9:
case 11:
this.day = 30;
break;
default:
this.month = 0;
Console.WriteLine("非法");
}
}
public int cal()
{
//实现计算的过程
}
}
//主函数的验证代码
if(input.IndexOf("-")!=-1)
{
string[] str = input.Split("-");
if (str == 3)
{
try
{
int y = int.Parse(str[0]);
}
catch (Exception ex)
{
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询