前三次作业总结
一. 前言
- 题目集一运用的多为基本语法语句和一些基础算法,题量较多但都比较简单
- 题目集二运用创建方法函数调用,与c语言的函数类似,题量中等,难度中等
- 题目集三开始运用创建类对象并运用对象解决问题,7-3需用到正则表达式解决,题量较少但比较难
二.设计与分析
- 题目集一 7-8流程图
![]()
- 题目集二7-4
![]()
1) 输入日期
2) 判断是否合法年份和月份
3) 判断是否闰年来决定二月是否合法的长度
4) 用if语句一步步判断是否合法
5) 用if循环输出下一天(独立判断12月份跨看是否年)
注:题目集三7-2类似题目但数组方法方便许多
- 题目集二7-5
![]()
/*与上一题类似*/
1) 判断输入日期是否合法(同上);
2) 判断n的正负进入不同循环
3) 运用循环求出n天后
- 题目集三7-2
1) 创建date类定义年月日
2) 创建数组大小为13,序号对应相应月份
3) 创建函数判断是否闰年若是闰年修改2的28为29

4) 用判断语句求出下一天

- 题目集三7-5

(需运用正则表达式,还在学习)
三.踩坑心得
- 题目集二7-4:未根据月份判断是否合法

题目集三7-2发现问题判断

四.改进建议
- 许多测试点过不去但不知道问题所在
如题目集三7-2
源码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Date date = new Date();
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
date.setter(year, month, day);
if(date.checkInputValidity())
{
date.getNextDate();
}
else
System.out.println("Date Format is Wrong");
}
}
class Date
{
private int year;
private int month;
private int day;
int[] mon_max = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
public Date()
{
}
void setter(int year,int month,int day)
{
this.day = day;
this.month = month;
this.year = year;
}
boolean isLeapYear()
{
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}
boolean checkInputValidity()
{
if(isLeapYear())
mon_max[2] = 29;
if(year >=1900 && year<= 2000 )
{
if(month < 0 || month > 12)
return false;
else
{
if(day < 0 || day > mon_max[month])
return false;
else
return true;
}
}
else
return false;
}
void getNextDate()
{
if(month == 12)
{
if(day == 31)
System.out.println("Next day is:"+(year+1)+"-"+1+"-"+1);
else
System.out.println("Next day is:"+year+"-"+month+"-"+(day+1));
}
else
{
if(day == mon_max[month])
System.out.println("Next day is:"+year+"-"+(month+1)+"-"+1);
else
System.out.println("Next day is:"+year+"-"+month+"-"+(day+1));
}
}
}
但运行结果

不知问题所在,希望以后能提示清楚
五.总结
三次作业让我熟悉方法和类的运用,进一步了解到java根据对象设计程序的思想
还需要学习一些更加高级的算法,对于一些类方法还不够了解,正则表达式也需要进一步学习
希望老师在布置作业时考虑到教学进度,有些题目难度太大无法下手




浙公网安备 33010602011771号