我需要编写一个代码来计算日期而不导入Date类。 首先,我需要输入有效日期 而且我不知道该怎么做。
Scanner scan = new Scanner(System.in);
int day,month,year,february;
System.out.println("please enter three nubers that represent original date");
day = scan.nextInt();
month = scan.nextInt();
year = scan.nextInt();
if( year <= 0 || day <= 0 || month <= 0 || month > 12 ){
System.out.println("The original date is " + day + "/" + month + "/" + year +
" is invalid");
}
if (year%100 == 0 || year % 4 != 0)
february = 28;
else if (day>28)
System.out.println("invalid date");
else if(year%400 == 0)
february = 29;
else if(day>29)
System.out.println("invalid date");
else if(year%4 == 0)
february = 29;
else if(day>29)
System.out.println("invalid date");
答案 0 :(得分:0)
您需要知道每个月的最大天数。最重要的任务是处理不是二月的月份。很难逐行解释,所以我希望代码能说明问题。 我可以缩短代码,但是我已经把它留下了,所以它更符合您的代码。 http://www.timeanddate.com/date/leapyear.html
//january = daysInMonth[0], february = daysInMonth[1]
int daysInMonth[] = new int[]
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int year = 1800;
int day = 30;
int month = 3;
if (month != 2) //Month is not February
{
int daysInEnteredMonth = daysInMonth[month - 1];
System.out.println("Days in entered month " + daysInEnteredMonth);
if (day > daysInEnteredMonth)
{
System.out.println("invalid date");
}
}
else
{
// Month is February
if (year % 4 != 0)
{
daysInMonth[1] = 28;
}
else if (year % 400 == 0)
{
daysInMonth[1] = 29;
}
else if (year % 100 == 0)
{
daysInMonth[1] = 28;
}
else //year is divisible by 4
{
daysInMonth[1] = 29;
}
//Do the comparison
int daysInEnteredMonth = daysInMonth[month - 1];
System.out.println("Days in entered month " + daysInEnteredMonth);
if (day > daysInEnteredMonth)
{
System.out.println("invalid date");
}
}
答案 1 :(得分:0)
一个简单的方法,在给定的年份检查是否是2月29日的一年(闰年,闰日)。
stepNetwork (Network x y z) = Network newX newY newZ
where
newX = stepLayer x (hOldResponse y)
newZ = stepLayer z (hOldResponse y)
newY = stepLayer y (currentResponse newX) (currentReponse newZ)