/*【程序14】 题目:输入某年某月某日,判断这一天是这一年的第几天? 1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。 */package test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;public class test { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入年月日:(格式2014-05-08)"); String input = br.readLine(); boolean flag = true; Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$"); while(flag) { Matcher matcher = pattern.matcher(input); if(!matcher.find()) { System.out.println("格式错误,请重新输入(格式2014-05-08)"); input = br.readLine(); } else { flag = false; } } final int []leap = {31,29,31,30,31,30,31,31,30,31,30,31}; final int []noleap = {31,28,31,30,31,30,31,31,30,31,30,31}; String date[] = input.split("-"); int year = Integer.parseInt(date[0]); int month = Integer.parseInt(date[1]); int day = Integer.parseInt(date[2]); int result = 0; if(year % 4 == 0) { for(int i = 0; i < month-1; i++) result = result + leap[i]; result += day; } else { for(int i = 0; i < month-1; i++) result = result + noleap[i]; result += day; } System.out.println("这是 "+year+"年的第 "+result+"天。"); }}