程式員必做的50道題之4:輸入某年某月某日,判斷這一天是這一年的第幾天?,50道某日
package pwd;/** * 程式員必做的50道題之4:輸入某年某月某日,判斷這一天是這一年的第幾天? * <p>@author SunJian</p> * <p>date:2017年10月20日</p> * <p>QQ:330160114</p> * <p>company:四海興唐</p> * <p>site:www.ccshxt.com</p> */public class DateCount {public static void main(String[] args) {//此處可以改為鍵盤輸入String input="20171020";//一年中的第多少天int howManyDates=0;//解析出年、月、日int year=Integer.parseInt(input.substring(0,4));int month=Integer.parseInt(input.substring(4,6));int date=Integer.parseInt(input.substring(6,8));boolean run=false;//判斷是否是閏年if((year % 100 ==0 && year % 4==0) && year % 400 !=0){run=true;}for(int m=1;m<month;m++){switch(m){case 1:howManyDates+=31;break;case 2:if(run){//閏年,二月29天howManyDates+=29;}else{//非閏年,二月28天howManyDates+=28;}break;case 3:howManyDates+=31;break;case 4:howManyDates+=30;break;case 5:howManyDates+=31;break;case 6:howManyDates+=30;break;case 7:howManyDates+=31;break;case 8:howManyDates+=31;break;case 9:howManyDates+=30;break;case 10:howManyDates+=31;break;case 11:howManyDates+=30;break;case 12:howManyDates+=31;break;default:System.out.println("錯誤");}}howManyDates=howManyDates+date;System.out.println(input+"是"+year+"年的第"+howManyDates+"天");}}