日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 判断日期为第几天

發(fā)布時間:2023/12/13 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 判断日期为第几天 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目1

編寫程序:從鍵盤上輸入2019年的“month”和“day”,要求通過程序 輸出輸入的日期為2019年的第幾天。

代碼1

從12月往下加日期數(shù)

package l1_switch_case; import java.util.Scanner; public class SwitchDemo4 {public static void main(String[]args){Scanner scanner=new Scanner(System.in);int month=scanner.nextInt();int day=scanner.nextInt();int date=0;/* //方式1:冗余switch (month){case 1:date=day;break;case 2:date=31+day;break;case 3:date=31+28+day;break;case 4:date=31+28+31+day;break;case 5:date=31+28+31+30+day;break;case 6:date=31+28+31+30+31+day;break;case 7:date=31+28+31+30+31+30+day;break;case 8:date=31+28+31+30+31+30+31+day;break;case 9:date=31+28+31+30+31+30+31+31+day;break;case 10:date=31+28+31+30+31+30+31+31+30+day;break;case 11:date=31+28+31+30+31+30+31+31+30+31+day;break;case 12:date=31+28+31+30+31+30+31+31+30+31+30+day;break;}*/switch (month){case 12:date+=31;case 11:date+=31;case 10:date+=30;case 9:date+=31;case 8:date+=31;case 7:date+=30;case 6:date+=31;case 5:date+=30;case 4:date+=31;case 3:date+=28;case 2://加上2月份前面一個月的天數(shù)date+=31;case 1:date+=day;}System.out.println(date);scanner.close();} }

題目2

從鍵盤分別輸入年、月、日,判斷這一天是當年的第幾天
注:判斷一年是否是閏年的標準:
可以被4整除,但不可被100整除

可以被400整除

代碼2-寫法1

package l1_switch_case;import java.util.Scanner;public class SwitchDemo5 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int year = scanner.nextInt();int month = scanner.nextInt();int day = scanner.nextInt();int date = 0;int february = 0;if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {february = 29;} else {february = 28;}switch (month){case 12:date+=31;case 11:date+=31;case 10:date+=30;case 9:date+=31;case 8:date+=31;case 7:date+=30;case 6:date+=31;case 5:date+=30;case 4:date+=31;case 3:date+=february;case 2://加上2月份前面一個月的天數(shù)date+=31;case 1:date+=day;}System.out.println(date);scanner.close();} }

代碼2-寫法2

package l1_switch_case;import java.util.Scanner;public class SwitchDemo6 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int year = scanner.nextInt();int month = scanner.nextInt();int day = scanner.nextInt();int date = 0;switch (month){case 12:date+=31;case 11:date+=31;case 10:date+=30;case 9:date+=31;case 8:date+=31;case 7:date+=30;case 6:date+=31;case 5:date+=30;case 4:date+=31;case 3:if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {date+= 29;} else {date+ = 28;}case 2://加上2月份前面一個月的天數(shù)date+=31;case 1:date+=day;}System.out.println(date);scanner.close();} }

總結(jié)

以上是生活随笔為你收集整理的java 判断日期为第几天的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。