今年的第几天?
输入年、月、日,计算该天是本年的第几天。
import java.util.Scanner;
import java.time.LocalDate;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
LocalDate dateTime = LocalDate.of(year,month,day);
System.out.println(dateTime.getDayOfYear());
}
}
} 邪修方法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 每个月的天数,平年
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 循环处理多组测试数据
while (scanner.hasNext()) {
int year = scanner.nextInt();
int month = scanner.nextInt();
int day = scanner.nextInt();
// 判断是否为闰年
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
// 计算总天数
int totalDays = 0;
// 累加前面月份的天数
for (int i = 0; i < month - 1; i++) {
totalDays += daysInMonth[i];
}
// 加上当月的天数
totalDays += day;
// 如果是闰年且月份大于2月,加1天
if (isLeapYear && month > 2) {
totalDays++;
}
System.out.println(totalDays);
}
scanner.close();
}
}
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
HashMap<Integer, Integer> pingYear = new HashMap<>();
pingYear.put(1, 31);
pingYear.put(2, 28);
pingYear.put(3, 31);
pingYear.put(4, 30);
pingYear.put(5, 31);
pingYear.put(6, 30);
pingYear.put(7, 31);
pingYear.put(8, 31);
pingYear.put(9, 30);
pingYear.put(10, 31);
pingYear.put(11, 30);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int day = 0;
for (Map.Entry<Integer, Integer> map : pingYear.entrySet()) {
int key = map.getKey();
int value = map.getValue();
if ( key > b - 1) {
day = day + c;
break;
}
day = day + value;
}
if (isRunYear(a)) {
if (b > 1) {
day += 1;
}
}
System.out.println(day);
}
}
public static boolean isRunYear(int year) {
return year % 4 == 0 && year % 100 != 0
|| year % 400 == 0;
}
} import java.util.Scanner;
import java.util.Calendar;
public class Main{
public static void main(String []args){
Scanner scan=new Scanner(System.in);
int year=scan.nextInt();
int month=scan.nextInt();
int day=scan.nextInt();
Calendar cal=Calendar.getInstance();
cal.set(year,month-1,day);
System.out.println(cal.get(Calendar.DAY_OF_YEAR));
}
}