java每日一练

题目:输入某年某月某日,判断这一天是这一年的第几天?

public class YearTest {

    private static int year;
    private static int month;
    private static int day;
    private static int[] ping = {31,28,31,30,31,30,31,31,30,31,30,31};
    private static int[] run = {31,29,31,30,31,30,31,31,30,31,30,31};

    public static void main(String[] args) {
        System.out.println("请输入年、月、日:");
        Scanner scanner = new Scanner(System.in);
        year = scanner.nextInt();
        month = scanner.nextInt();
        day = scanner.nextInt();
        isYear();

    }

    public static void isYear(){
        int sum = 0,num = 0,i=0;
        if(year%4==0&&year%100==0||year%400==0){
            System.out.println("闰年");
            for(;i1;i++){
                int m = run[i];
                sum =sum+m;
                if(day>run[i+1]){
                    System.out.println("超出当月天数");
                    System.exit(0);
                }
            }
            num = sum+day;
            System.out.println(year+"/"+month+"/"+day+"是"+year+"年的第"+num+"天");
        }else{
            System.out.println("平年");
            for(;i1;i++){
                int m = ping[i];
                sum =sum+m;
                if(day>ping[i+1]){
                    System.out.println("超出当月天数");
                    System.exit(0);
                }
            }
            num = sum+day;
            System.out.println(year+"/"+month+"/"+day+"是"+year+"年的第"+num+"天");
        }
    }

}

你可能感兴趣的:(Java每日一练)