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

package lianxi;

import java.util.*;

public class Panduantianshu {

    public static void main(String[] args) {
        int sum;
        while (true)
        {
            System.out.println("请输入年份:");
            Scanner shu1 = new Scanner(System.in);
            int x = shu1.nextInt();
            if (x > 9999 || x < 1759)
            {

                System.out.println("您输入的年份有误,请重新输入年份");
                continue;
            } 
            else 
            {
                while (true) 
                {
                    System.out.println("请输入月份:");
                    Scanner shu2 = new Scanner(System.in);
                    int y = shu2.nextInt();
                    if (y > 12 || y < 1) {

                        System.out.println("您输入的月份有误,请重新输入月份");
                        continue;
                    } 
                    else
                    {
                        while (true) 
                        {
                            System.out.println("请输入日期:");
                            Scanner shu3 = new Scanner(System.in);
                            int z = shu3.nextInt();
                            if (z < 0) {
                                System.out.println("您输入的日期有误,请重新输入日期");
                                continue;
                            } 
                            else if ((y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
                                    && z > 31) {

                                System.out.println("您输入的日期有误,请重新输入日期");
                                continue;
                            } 
                            else if ((y == 4 || y == 6 || y == 9 || y == 11) && z > 30) {
                                System.out.println("您输入的日期有误,请重新输入日期");
                                continue;
                            } 
                            else if ((x % 400 == 0 || (x % 100 != 0 && x % 4 == 0)) && y == 2 && z > 29) {
                                System.out.println("您输入的日期有误,请重新输入日期");
                                continue;
                            } 
                            else if (!(x % 400 == 0 || (x % 100 != 0 && x % 4 == 0))&&y == 2 && z > 28) {
                                System.out.println("您输入的日期有误,请重新输入日期");
                                continue;
                            } 
                            else {
                                if (x % 400 == 0 || (x % 100 != 0 && x % 4 == 0)) {
                                    if (y < 2) {
                                        sum = z;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    }
                                    if (y == 2) {

                                        sum = z + 31;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    }
                                
                                if (y > 2 && y < 8) {
                                    if (y % 2 == 0) {
                                        sum = y / 2 * 31 + (y / 2 - 2) * 30 + 29 + z;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    }
                                    if (y % 2 != 0) {
                                        sum = (y - 1) / 2 * 31 + ((y - 1) / 2 - 1) * 30 + 29 + z;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    }
                                }
                                if (y >= 8) {
                                    if (y % 2 == 0) {
                                        sum = (y - 8) / 2 * 31 + (y - 8) / 2 * 30 + 31 * 4 + 30 * 2 + 29 + z;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    } 
                                    else {
                                        sum = (y - 7) / 2 * 31 + ((y - 7) / 2 - 1) * 30 + 31 * 4 + 30 * 2 + 29 + z;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    }
                                }
                            }

                                else {
                                    if (y < 2) {
                                        sum = z;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;
                                    }
                                    if (y == 2) {

                                        sum = z + 31;
                                        System.out.println("这是这一年的第" + sum + "天");
                                        break;

                                    }
                                    if (y > 2 && y < 8) {
                                        if (y % 2 == 0) {
                                            sum = y / 2 * 31 + (y / 2 - 2) * 30 + 28 + z;
                                            System.out.println("这是这一年的第" + sum + "天");
                                            break;
                                        } else {
                                            sum = (y - 1) / 2 * 31 + ((y - 1) / 2 - 1) * 30 + 28 + z;
                                            System.out.println("这是这一年的第" + sum + "天");
                                            break;
                                        }
                                    }
                                    if (y >= 8) {
                                        if (y % 2 == 0) {
                                            sum = (y - 8) / 2 * 31 + ((y - 8) / 2) * 30 + 31 * 4 + 28 + 30 * 2 + z;
                                            System.out.println("这是这一年的第" + sum + "天");
                                            break;
                                        } 
                                        else {
                                            sum = (y - 7) / 2 * 31 + ((y - 7) / 2 - 1) * 30 + 31 * 4 + 28 + 30 * 2 + z;
                                            System.out.println("这是这一年的第" + sum + "天");
                                            break;
                                        }
                                      
                                    }
                                }

                            break;}
                        }

                    break;}
                }             
        
           break;}
        
     }
}
}

 

posted @ 2016-05-17 15:21  凌零聆  阅读(649)  评论(0编辑  收藏  举报