A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© danielchung6600 中级黑马   /  2016-6-25 00:44  /  857 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我们刚学第五天的课程,只会用if switch 和for,求大神指导,如何编写万年历

8 个回复

倒序浏览
/**  * 需求:编写一个万年历  * 分析:其实记住小学老师教的口诀就可以了  *      一三五七八十腊,四六九冬三十天,  *      平年二月二十八,闰年二月二十九,  *      平年三百六十五,闰年三百六十六。  * 注解:腊月也就是十二月,冬月也就是十一月。  * @author Administrator  *  */ public class Test4 { public static void main(String[] args) {         Scanner in=new Scanner(System.in);           System.out.println("请输入年份:");           int nian=in.nextInt();           System.out.println("请输入月份");          int yue=in.nextInt();           int sum=0;           //计算从1900年到输入的年份有多少天         for(int year=1900;year<=(nian-1);year++){                  //根据输入的年份判断是否是闰年或者是平年                 if(year%4==0&&year%100!=0||year%400==0){                           sum=sum+366;   //闰年366天                         }else{                                 sum=sum+365;    //平年365天                                 }                    }          //计算从一月到输入的月份有多少天         for(int month=1;month<=(yue-1);month++){                  if(month==2){                         if(nian%4==0&&nian%100!=0||nian%400==0){                                   sum+=29;   //闰年二月29                                 }else {                                            sum+=28;    //平年二月28                                         }                             }else {                                     //4  6  9  冬(11)三十天                                 if(month==4||month==6||month==9||month==11){                                           sum+=30;                                             }else{                                                      sum+=31;  //1 3 5 7 9 10 腊(12)三十一天                                                  }                                    }                       }                            sum=sum+1; //从每月的1号开始排列                    int weekday=sum%7;   //一个星期七天                         System.out.println("日\t一\t二\t三\t四\t五\t六");                   for(int i=1;i<=weekday;i++){                          System.out.print("\t");                  }                   for(int i=1;i<=30;i++){                           if(sum%7==6){   //每到周六换行显示                         System.out.print(i+"\n");                          }else {                                   System.out.print(i+"\t");                         }                 sum++;                 } } }
回复 使用道具 举报
上面的代码太乱了,再来一遍!
/**
* 需求:编写一个万年历
* 分析:其实记住小学老师教的口诀就可以了
*      一三五七八十腊,四六九冬三十天,
*      平年二月二十八,闰年二月二十九,
*      平年三百六十五,闰年三百六十六。
* 注解:腊月也就是十二月,冬月也就是十一月。
* @author Administrator
*
*/
public class Test4 {
public static void main(String[] args) {
        Scanner in=new Scanner(System.in);  
        System.out.println("请输入年份:");  
        int nian=in.nextInt();  
        System.out.println("请输入月份");
        int yue=in.nextInt();  
        int sum=0;  
        //计算从1900年到输入的年份有多少天
        for(int year=1900;year<=(nian-1);year++){
                //根据输入的年份判断是否是闰年或者是平年
                if(year%4==0&&year%100!=0||year%400==0){  
                        sum=sum+366;   //闰年366天
                        }else{
                                sum=sum+365;    //平年365天
                                }   
                }
        //计算从一月到输入的月份有多少天
        for(int month=1;month<=(yue-1);month++){
                if(month==2){
                        if(nian%4==0&&nian%100!=0||nian%400==0){  
                                sum+=29;   //闰年二月29
                                }else {   
                                        sum+=28;    //平年二月28
                                        }   
                        }else {   
                                //4  6  9  冬(11)三十天
                                if(month==4||month==6||month==9||month==11){  
                                        sum+=30;   
                                        }else{     
                                                sum+=31;  //1 3 5 7 9 10 腊(12)三十一天
                                                }   
                                }      
                }   
                        sum=sum+1; //从每月的1号开始排列   
                int weekday=sum%7;   //一个星期七天
                        System.out.println("日\t一\t二\t三\t四\t五\t六");  
                for(int i=1;i<=weekday;i++){
                        System.out.print("\t");
                }  
                for(int i=1;i<=30;i++){  
                        if(sum%7==6){   //每到周六换行显示
                        System.out.print(i+"\n");
                        }else {  
                                System.out.print(i+"\t");
                        }
                sum++;
                }
}
}
回复 使用道具 举报 1 0
可以可以
回复 使用道具 举报
貂裘换酒 发表于 2016-6-25 10:23
上面的代码太乱了,再来一遍!
/**
* 需求:编写一个万年历

感谢大神
回复 使用道具 举报
import java.util.Scanner;
class wan {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("输入一个年份");
                int year = sc.nextInt();
                System.out.println("输入一个月份");
                int month = sc.nextInt();
                int yearday = 0 ;
                if (year / 400 ==0) {
                        yearday = 366;
                        System.out.println(year+"是闰年");
                }else if (year % 100 != 0 && year % 4 == 0) {
                        yearday = 366;
                        System.out.println(year+"是闰年");
                }else {
                        yearday = 365;
                        System.out.println(year+"是平年");
                }
                //获取输入年份到1900年的总天数
                int z = 0;
                for (int a = 1900;a < year ;a++ ) {
                        if (a / 400 ==0) {
                        z += 366;
                        }else if (a % 100 != 0 && a % 4 == 0) {
                                z += 366;
                        }else {
                                z += 365;
                        }
                }
               
                int y = 0;
                for (int i = 1;i < month ;i++ ) {
                         y += getDay(i , year);
                }
                System.out.println("一\t二\t三\t四\t五\t六\t日");
                int week = (z + y) % 7 + 1;
                for (int x = 1 ;x <= week ;x++ ) {
                        if (week ==7) {
                                break;
                        }
                        System.out.print("\t");
                }
                for (int x = 1;x <= getDay(month , year) ; x++ ) {
                        System.out.print(x+"\t");
                                if ((x + week)%7==0) {
                                        System.out.println();
                                }
                        }
                }
        //获取输入月份的天数
        public static int getDay(int a , int b){
                int day = 0;
                switch (a) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                        day =31;
                break;
                case 4:
                case 6:
                case 9:
                case 11:
                        day = 30;
                break;
                case 2:
                        if ( b / 400 ==0) {
                                day = 29;
                        }else if (b % 100 != 0 && b % 4 == 0) {
                                day = 29;
                        }else {
                                day = 28;
                        }
                break;
                }
                return day;       
        }
}
回复 使用道具 举报 1 0
上面的大神们真是6的飞起
回复 使用道具 举报
我好想偶遇了大神呀,双击评论666
回复 使用道具 举报

不用谢!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马