标签:
题目中涉及到很多知识点,包括强制类型转换,字符串截取,闰年的判断方法
题目描述:
1985/1/20 2006/3/12
20 71
AC代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { //定义一年12个月的每个月天数 int s=0,b[] = {31,28,31,30,31,30,31,31,30,31,30,31}; String a; a = in.nextLine(); //强制字符串截断,以“/”位标识 int x = a.indexOf("/"); int y = a.lastIndexOf("/"); //截取输入字符串中年月日 String year = a.substring(0,x); String month = a.substring(x+1,y); String day = a.substring(y+1); //强制类型转换,字符串转换为整型 int year1 = Integer.parseInt(year); int month1 = Integer.parseInt(month); int day1 = Integer.parseInt(day); if(year1%400==0||(year1%4==0&&year1%100!=0))//判断年份是否为闰年 { //闰年二月29天 b[1] = b[1]+1; for(int i=0;i<month1-1;i++) { s =s+ b[i]; } System.out.println(s+day1); } else { for(int i=0;i<month1-1;i++) { s =s+ b[i]; } System.out.println(s+day1); } } } }
标签:
原文地址:http://blog.csdn.net/chenwewei/article/details/51334812