public static boolean yesNoRunNian(int year)。。。 这个方法就是判断闰年的。我复制我自己做的题,仅供参考
package text.lianxi;
import java.util.Scanner;
//输入某年某月某日,判断这一天是这一年的第几天?
public class lianxi14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//输入年份
System.out.println("请输入一个年份:");
int year = input.nextInt();
//输入月份
System.out.println("请输入一个月份:");
int month = input.nextInt();
//输入日子
System.out.println("请输入一个年份:");
int day = input.nextInt();
boolean boo = yesNoRunNian(year);
int sumDay = getday(boo,month);
System.out.println(sumDay+day);
}
public static boolean yesNoRunNian(int year){
boolean flag = false;
if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){
flag = true;
}
return flag;
}
public static int getday(boolean year,int month){
int[] arr1 ={31,28,31,30,31,30,31,31,30,31,30,31};
int[] arr2 ={31,29,31,30,31,30,31,31,30,31,30,31};
int sum = 0;
if(year){
for (int i = 0; i < month-1; i++) {
sum += arr1[i];
}
}else{
for (int i = 0; i < month-1; i++) {
sum += arr2[i];
}
}
return sum;
}
} |