思路: 1,分別定義三個變量來接收 年 月 日 2,累加已經(jīng)過完的月份的天數(shù) + 日期 3,二月份的天數(shù)要根據(jù)是否是閏年,隨之改變 1 3 5 7 8 10 12 ---> 31天 4 6 9 11 ---> 30天 2 ---> 閏年:29 平年:28
代碼:
public class YearMonthDay { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("請輸入年份:"); int year = sc.nextInt(); System.out.println("請輸入月份:"); int month = sc.nextInt(); System.out.println("請輸入幾日:"); int day = sc.nextInt(); int sum =0; for (int i=1;i<month;i++){ //i是已經(jīng)過完的月份 if (i==1 || i==3 || i==5|| i==7||i==8||i==10||i==12){ sum+=31; } else if (i==2) { if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){ sum+=29; }else { sum+=28; } }else { sum+=30; } } sum+=day; System.out.println(year+"-"+month+"-"+day+"是當前年的第"+sum+"天");????????}文章來源:http://www.zghlxwxcb.cn/news/detail-821112.html
}文章來源地址http://www.zghlxwxcb.cn/news/detail-821112.html
?效果圖:
?這是初級階段的求法,學過?LocalDate的可以直接用?LocalDate求一年中的第幾天
?public class Main {
????????public static void main(String[] args) {
????????????????// 創(chuàng)建一個 LocalDate 對象,表示特定的日期 ???????????????
?????????????????LocalDate date = LocalDate.of(2024, 1, 23);
????????????????// 獲取一年中的第幾天
????????????????int dayOfYear = date.getDayOfYear(); System.out.println("Day of year: " + dayOfYear);
????????}
}
到了這里,關于輸入某年某月某日,判斷這一天是這一年的第幾天?(Java)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!