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

方法一:
package ClassTest;
import java.util.Scanner;
/**
 * 输入某年某月某日,判断这一天是这一年的第几天?
 * @author Administrator
 *
 */
public class Demo015 {
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
System.out.print("请输入年:");
int year = in.nextInt();
System.out.print("请输入月:");
int month = in.nextInt();
System.out.print("请输入日:");
int day = in.nextInt();
switch (month) {
case 12:
count += 30;
case 11:
count += 31;
case 10:
count += 30;
case 9:
count += 31;
case 8:
count += 30;
case 7:
count += 31;
case 6:
count += 31;
case 5:
count += 30;
case 4:
count += 31;
case 3:
count += 28;
case 2:
count += 31;
case 1:
count += 0;
}
count += day;
if (year % 4 == 0 && year % 100 != 0 && month >= 3) {
count += 1;
}
System.out.print(year + "-" + month + "-" + day + "是一年中的第" + 


count
+ "天");
}
}




方法二:
package ClassTest;
import java.util.Scanner;
/**
 * 输入某年某月某日,判断这一天是这一年的第几天?
 * @author Administrator
 *
 */
public class Demo016 {
public static void main(String[] args) throws Exception {
int y, m, d;
int sum = 0;
int feb = 28;
Scanner in = new Scanner(System.in);
System.out.print("请输入年份:");
y = in.nextInt();
System.out.print("请输入月份:");
m = in.nextInt();
System.out.print("请输入几号:");
d = in.nextInt();


if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
feb = 29;
}


switch (m) {
case 1:
sum = d;
break;
case 2:
sum = 31 + d;
break;
case 3:
sum = 31 + feb + d;
break;
case 4:
sum = 31 + feb + 31 + d;
break;
case 5:
sum = 31 + feb + 31 + 30 + d;
break;
case 6:
sum = 31 + feb + 31 + 30 + 31 + d;
break;
case 7:
sum = 31 + feb + 31 + 30 + 31 + 30 + d;
break;
case 8:
sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + d;
break;
case 9:
sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + d;
break;
case 10:
sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + d;
break;
case 11:
sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;
break;
case 12:
sum = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 


d;
}
System.out.println(y + " 年 " + m + " 月 " + d + "号是今年的第" + sum 


+ "天");
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值