小城市网站建设,中山 网站建设,ui界面设计实例100例,龙岗网站改版思路#xff1a;
1#xff0c;分别定义三个变量来接收 年 月 日
2#xff0c;累加已经过完的月份的天数 日期
3#xff0c;二月份的天数要根据是否是闰年#xff0c;随之改变
1 3 5 7 8 10 12 --- 31天
4 6 9 11 --- 30天
2 --- 闰…思路
1分别定义三个变量来接收 年 月 日
2累加已经过完的月份的天数 日期
3二月份的天数要根据是否是闰年随之改变
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 i1;imonth;i){ //i是已经过完的月份if (i1 || i3 || i5|| i7||i8||i10||i12){sum31;} else if (i2) {if((year % 400 0) || (year % 4 0 year % 100 ! 0)){sum29;}else {sum28;}}else {sum30;}}sumday;System.out.println(year-month-day是当前年的第sum天); } } 效果图: 这是初级阶段的求法,学过 LocalDate的可以直接用 LocalDate求一年中的第几天 public class Main { public static void main(String[] args) { // 创建一个 LocalDate 对象表示特定的日期 LocalDate date LocalDate.of(2024, 1, 23); // 获取一年中的第几天 int dayOfYear date.getDayOfYear(); System.out.println(Day of year: dayOfYear); } }