程序思路:
1.让用户从键盘输入想要判断的日期
2.然后判断日期是否正确,保存入文件in.txt中:
用正则表达式判断日期格式是否正确
判断输入的年、月、日是否正确
3.从文件in.txt中读取出数据,计算从2010年1月1日开始至指定日期共有多少天
4.判断指定日期是在打鱼还是在晒网并将结果保存到out.txt中
程序流程图
下面是程序全部代码:
import java.io.*;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* 从键盘中读取日期并且存入in.txt中
* @author 赵卓屹
*/
class PutInData{
private String date;
//构造函数从键盘获取数据并且储存在in.txt中。
public PutInData() throws Exception{
System.out.println("请输入要判断的日期:");
Scanner input =new Scanner(System.in);
date = input.nextLine();
if(!Pattern.matches("\\d{8}", date)){
throw new Exception("日期格式不正确!");
}
FileWriter writer = new FileWriter("in.txt");
writer.write(date);
writer.write("\r\n");
input.close();
writer.close();
}
}
/**
* 从in.txt中获取日期
* 算出距2010年1月1日有多少天
* 推出这一天是在打鱼还是在晒网
* 将结果保存在out.txt中
* @author 赵卓屹
*/
class GetData{
private int year;
private int month;
private int day;
private final int MONTH_DAYS[][] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
//构造函数从in.txt获取数据并且赋值给year,month,day并且判断数据是否正确。
public GetData() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("in.txt"));
String line = reader.readLine();
year = Integer.parseInt(line.substring(0, 4));
month = Integer.parseInt(line.substring(4, 6));
day = Integer.parseInt(line.substring(6, 8));
if (year < 2010){
System.out.println("输入的年份请大于或等于2010!");
System.exit(0);
}
if(month>12||month<0){
System.out.println("输入的月份请在0~12之间!");
System.exit(0);
}
if(isLeapYear(year)){
if((month==1&&day>31)||(month==2&&day>29)||(month==3&&day>31)||(month==4&&day>30)||(month==5&&day>31)||(month==6&&day>30)||(month==7&&day>31)||
(month==8&&day>31)||(month==9&&day>30)||(month==10&&day>31)||(month==11&&day>30)||(month==12&&day>31)){
System.out.println("输入的日期有误!");
System.exit(0);
}
}
if(!isLeapYear(year)){
if((month==1&&day>31)||(month==2&&day>28)||(month==3&&day>31)||(month==4&&day>30)||(month==5&&day>31)||(month==6&&day>30)||(month==7&&day>31)||
(month==8&&day>31)||(month==9&&day>30)||(month==10&&day>31)||(month==11&&day>30)||(month==12&&day>31)){
System.out.println("输入的日期有误!");
System.exit(0);
}
}
}
// 判断是否为闰年
public boolean isLeapYear(int year){
if(year%400==0||(year%4==0&&year%100!=0))
return true;
else
return false;
}
//计算出距离2010年1月1日的天数
public int getDays(){
int days = 0;
int startYear = 2010;
if(year>2010){
for(;startYear<year;startYear++){
if(isLeapYear(startYear)){
for(int i=0;i<=12;i++)
days += MONTH_DAYS[1][i];
}
else
for(int i=0;i<=12;i++)
days += MONTH_DAYS[0][i];
}
for(int i=1;i<month;i++)
days += MONTH_DAYS[0][i];
}
else
for(int i=1;i<month;i++)
days += MONTH_DAYS[0][i];
days+=day;
System.out.println("这一天与2010年1月1日相差"+days+"天");
return days;
}
// 判断这一天是打鱼还是晒网
public void fishingOrDrying() throws IOException{
int result = getDays()%5;
if(result==1||result==2||result==3){
System.out.println("这一天在打鱼");
FileWriter writer = new FileWriter("out.txt");
writer.write("这一天在打鱼");
writer.write("\r\n");
writer.close();
}
else{
System.out.println("这一天在晒网");
FileWriter writer = new FileWriter("out.txt");
writer.write("这一天在晒网");
writer.write("\r\n");
writer.close();
}
}
}
public class Judge {
public static void main(String[] args) throws Exception {
new PutInData();
new GetData().fishingOrDrying();
}
}
总结:
正则表达式
String类中的三个基本操作使用正则:
匹配:matches()
切割: split()
替换: replaceAll()
\d | 匹配一个数字字符。等价于[0-9]。 |
\D | 匹配一个非数字字符。等价于[^0-9]。 |
/**
* java中正则匹配的对象:
* pattern:
* Pattern Pattern.complie(regexString)
* Macther Pattern.matches(regexString)
* Matcher:
* boolean matcher.find() //查找下一个匹配对象
* String matcher.guorp() //返回整个匹配模式匹配到的结果
* boolean matcher.matches() //尝试将整个区域与模式匹配
* int matcher.groupCount() //返回匹配规则的分组,如:(aa)(bb):这表示两组
* String matcher.group(int group) //返回匹配对象对应分组的匹配结果
* MatcheResult matcher.toMatchResult() //将匹配结果一MatchResult的形式返回
*/