码迷,mamicode.com
首页 > 其他好文 > 详细

万年历

时间:2017-09-25 22:03:37      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:blog   pack   pre   div   nbsp   static   exti   cas   tin   

package 实验二;
import java.util.Scanner;
public class wannianli {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    for(;;)
    {
        System.out.println("1.判断闰年");
        System.out.println("2.判断某年某月一共有多少天");
        System.out.println("3.判断某年某月距离1900-1-1有多少天");
        System.out.println("4.输出某年某月日历");
        System.out.println("请输入你的选择");
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        switch(x)
        {
        case 1:
            int y=xierunian();
            if(runnian(y))
                System.out.println("是闰年");
            else
                System.out.println("不是闰年");
            break;
        case 2:
            System.out.println("一共有:"+getdays());
            break;
        case 3:
            int j=xierunian();
            int j1=xieruyue();
            int k=zongday(j,j1);
            System.out.println("一共有"+k);
            break;
        case 4:
            printrili();
            break;
        default:
            break;
        }
        System.out.println("是否要继续"+"是 按1"+"不是按2");
        Scanner sc1=new Scanner(System.in);
        int y=sc1.nextInt();
        if(y==2)
        {
            System.exit(1);
        }
        }
        
    }
    
    public static boolean runnian(int x)   //判断是否是闰年
    { 
        if(x%4==0&&x%100!=0||x%400==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static int getday(int year,int x)  //得到每年不同月份的天数
    {
        switch(x)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
            return 31;
            case 4:
            case 6:
            case 9:
            case 11:
            return 30;
            case 2:
            {
                if(runnian(year))
                    return 29;
                else
                    return 28;
            }
            default:
                return 0;
        }
    }
    public static int xierunian()  //写入年份
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入年份");
        int year=sc.nextInt();
        return year;
    }
    public static int xieruyue()  //写入月份
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入月份");
        int month=sc.nextInt();
        return month;
    }
    public static int getdays()  //判断某年某月有多少天
    {
        int x=xierunian();
        int y=xieruyue();
        int z=getday(x,y);
        return z;
    }
    public static int zongday(int x,int y)  //判断某年某月距离1900年一月一日多少天
    { 
        int m=0,n=0,g=0;
        if(x==1900)
        { 
            for(int j=1;j<=y;j++)
        {
            int k=getday(x,j);
            g+=k;
            return g;
        }
        }
        for(int z=1900;z<x;z++)
        {
            if(runnian(z))
                m+=366;
            else
                n+=365;
        }
        for(int j=1;j<=y;j++)
        {
            int k=getday(x,j);
            g+=k;
        }
        return m+n+g;
    }
    public static  void print()
    {
        System.out.println("==========");
        System.out.println("日\t"+"一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六\t");
    }
    public static void printrili()
    { 
        int count=0;
        int x=xierunian();
        int y=xieruyue();
        int m=getday(x,y);
        int z=zongday(x,y);
        int k=z%7;
        print();
        if(x==1900&&y==1)
        {
            System.out.print("\t");
            count=1;
            for(int j=1;j<=31;j++)
            { 
                System.out.print(+j+"\t");
                count++;
                if(count%7==0)
                    System.out.println("\n");
            }
            
        }
        else
        {
        if(k!=0)
        {
        for(int j=1;j<=k;j++)
        {
            System.out.print("\t");
            count++;
        }
        for(int j2=1;j2<=m;j2++)
        {
            System.out.print(+j2+"\t");
            count++;
            if(count%7==0)
            {
                System.out.print("\n");
            }
        }
        }

        else
        {
            for(int j2=1;j2<=m;j2++)
            {
                System.out.print(+j2+"\t");
                count++;
                if(count%7==0)
                {
                    System.out.println("\n");
                }
            }
        }
        }
        }
    }

 

万年历

标签:blog   pack   pre   div   nbsp   static   exti   cas   tin   

原文地址:http://www.cnblogs.com/jiazehao/p/7593810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!