重现代码
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
int year, month, day;
String str = buf.readLine();
year = Integer.parseInt(str);
str = buf.readLine();
month = Integer.parseInt(str);
str = buf.readLine();
day = Integer.parseInt(str);
int total = 0;
int[] pinDaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] runDaysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i = 0; i < month - 1; i++) {
if(year%4==0){
total += runDaysInMonth[i];
}
else{
total +=pinDaysInMonth[i];
}
}
total += day;
System.out.println("Total days: " + total);
}
}
这段代码的数据读取是逐行读取导致清览题库的检测数据过不了,因为他的检测数据是2021 11 10
这样子的数据,导致输出Exception in thread "main" java.lang.NumberFormatException: For input string: "2021 11 10" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at Main.main(Main.java:8)
修改方式
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] inputs = buf.readLine().split(" ");
int year = Integer.parseInt(inputs[0]);
int month = Integer.parseInt(inputs[1]);
int day = Integer.parseInt(inputs[2]);
int total = 0;
int[] pinDaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] runDaysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i = 0; i < month - 1; i++) {
if(year%4==0){
total += runDaysInMonth[i];
}
else{
total +=pinDaysInMonth[i];
}
}
total += day;
System.out.println("Total days: " + total);
}
}