import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); int month = scanner.nextInt(); int day = scanner.nextInt(); LocalDate date = LocalDate.of(year, month, day); LocalDate date01 = LocalDate.of(year, 1, 1); long days = date01.until(date, ChronoUnit.DAYS); System.out.println(days+1); } }
普通方法
import java.util.*; public class Main{ public static final int[] mon={ 0,31,28,31,30,31,30,31,30,31,30,31,30 }; public static void main(String[] args) { //如果是闰年, 2月是 29天, 否则 是 2天 Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int month = sc.nextInt(); int day = sc.nextInt(); if( year%400==0||(year%100!=0&&year%4==0) ) { mon[2] = 29; } int res = 0; for(int i=1;i<month;++i) { res+=mon[i]; } res+=day; System.out.println(res); } }