首页 > 编程学习 > 天数计算

天数计算

发布时间:2022/2/7 9:33:34

题目

问题描述
  编写函数求某年某月某日(**** ** **)是这一年的第几天 。提示:要考虑闰年,闰年的2月是29天(闰年的条件:是4的倍数但不是100的倍数,或者是400的倍数)。编写主函数,输入年月日,测试该函数并输出结果。
输入格式
  按“yyyy mm dd"的格式输入年月日
输出格式
  输出一个整数形式的计算结果
样例输入
1990 5 10
样例输出
130
数据规模和约定
  根据实际输入合法的日期
import java.util.Scanner;

public class 天数计算 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int year=sc.nextInt();
		int month=sc.nextInt();
		int date=sc.nextInt();
		int sum=0;
		if(year%400==0||year%4==0&&year%100!=0){
			switch(month){
			case 1:
				sum=date;
				break;
			case 2:
				sum=31+date;
				break;
			case 3:
				sum=31+29+date;
				break;
			case 4:
				sum=31+29+date+31;
				break;
			case 5:
				sum=31+29+date+31+30;
				break;
			case 6:
				sum=31+29+date+31+30+31;
				break;
			case 7:
				sum=31+29+date+31+30+31+30;
				break;
			case 8:
				sum=31+29+date+31+30+31+30+31;
				break;
			case 9:
				sum=31+29+date+31+30+31+30+31+31;
				break;
			case 10:
				sum=31+29+date+31+30+31+30+31+31+30;
				break;
			case 11:
				sum=31+29+date+31+30+31+30+31+31+30+31;
				break;
			case 12:
				sum=31+29+date+31+30+31+30+31+31+30+31+30;
				break;
			}
		}else{
			switch(month){
			case 1:
				sum=date;
				break;
			case 2:
				sum=31+date;
				break;
			case 3:
				sum=31+28+date;
				break;
			case 4:
				sum=31+28+date+31;
				break;
			case 5:
				sum=31+28+date+31+30;
				break;
			case 6:
				sum=31+28+date+31+30+31;
				break;
			case 7:
				sum=31+28+date+31+30+31+30;
				break;
			case 8:
				sum=31+28+date+31+30+31+30+31;
				break;
			case 9:
				sum=31+28+date+31+30+31+30+31+31;
				break;
			case 10:
				sum=31+28+date+31+30+31+30+31+31+30;
				break;
			case 11:
				sum=31+28+date+31+30+31+30+31+31+30+31;
				break;
			case 12:
				sum=31+28+date+31+30+31+30+31+31+30+31+30;
				break;
			}
		}
		System.out.println(sum);
	}
}

Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000