输入年月日,判断这一天是该年的第几天

public class Java05 {
	//输入年月日,判断这一天是该年的第几天
	public static void main(String[] args) {
		int year = 0;   //年
		int month = 0;  //月
		int day = 0;    //日
		int sum = 0;    //合计天数
		int flg = 0;    //判断输入正确与否
		
		Scanner sc = new Scanner(System.in);
		//读取输入的年月日
		do {
			System.out.println("请输入年:");
			year = sc.nextInt();
			System.out.println("请输入月:");
			month = sc.nextInt();
			System.out.println("请输入日:");
			day = sc.nextInt();
			
			if (month < 1 || month > 12 || day < 1 || day > 31) {
				System.out.println("请输入正确的日期");
				flg = 1;
			}
		} while (flg == 1);
		sc.close();
		
		//取得天数的和
		for (int i = 1; i < month; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				sum+=31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				sum+=30;
				break;
			case 2:
				//闰年的2月29天,平年28天
				if (((year%4 == 0 && year%100 != 0) || year%400 == 0)) {
					sum+=29;
				}else{
					sum+=28;
				}
			default:
				break;
			}
		}
		sum+=day;
		System.out.println(year+"年"+month+"月"+day+"号"+"是该年的"+sum+"天");
	}
}
for循环那里,不能写i<=month,不然会多出一个月的天数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值