编程语言
首页 > 编程语言> > java计算某一天是这一年的第几天

java计算某一天是这一年的第几天

作者:互联网

switch-case小练习
计算某一天是这一年的第几天

/*
switch-case 输入年月日,计算日期是这一年的第几天
(ps:未考虑输入非法情况)
*/
import java.util.Scanner;
class SwitchExer{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入year:");
		int year = scan.nextInt();
		System.out.println("请输入month:");
		int month = scan.nextInt();
		System.out.println("请输入day:");
		int day = scan.nextInt();
		//定义一个变量存放计算结果
		int sumDay = 0;
		switch(month){
			case 12:
				sumDay += 30;
			case 11:
				sumDay += 31;
			case 10:
				sumDay += 30;
			case 9:
				sumDay += 31;
			case 8:
				sumDay += 31;
			case 7:
				sumDay += 30;
			case 6:
				sumDay += 31;
			case 5:
				sumDay += 30;
			case 4:
				sumDay += 31;
			case 3:
				if ((year%4 == 0&&year%100 != 0) || year%400 == 0){
					sumDay += 29;
				}else
					sumDay += 28;
			case 2:
				sumDay += 31;
			case 1:
				sumDay += day;
		}
		System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDay + "天");
	}

}

标签:case,java,第几天,scan,int,31,sumDay,System,计算
来源: https://blog.csdn.net/wuwangzeguo/article/details/112434044