【程序14】
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天
import java.util.Scanner;
public class B_14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入年:");
int year = sc.nextInt();
System.out.print("请输入月:");
int month = sc.nextInt();
System.out.print("请输入日:");
int day = sc.nextInt();
int i ;
int numberday = 0;
int []number = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
//关于闰年与平年的算法网上有很对说法,此处采取一种最常见的
if((year%400)==0 || ((year%4)==0)&&((year%100)!=0)){
number[1] = 29;
}
for(i=0;i<month-1;i++){
numberday += number[i];
}
numberday += day;
System.out.print(year+"年"+month+"月"+day+"日是 "+year+"年的第 "+numberday+" 天");
}
}