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

虽然我分够了,不过还是来看看吧
回复 使用道具 举报
看看会不会
回复 使用道具 举报
支持一下,看看
回复 使用道具 举报
好难的题目
回复 使用道具 举报
试试吧,检验一下自己。
回复 使用道具 举报
飘过来瞄一眼看看能回答不
回复 使用道具 举报
为了做题,老板安排加班都不去了,加油!进黑马
回复 使用道具 举报
来尝试下~
回复 使用道具 举报
GOGOGOGOGO
回复 使用道具 举报

/**
* 题目:
*  输入某年某月,判断这一年与这一月分别距离1898年1月1日多少天?
   程序分析:以2013年7月为例,使用循环计算1898年1月1日距离2013年1月1日的总天数(考虑闰年与平年的情况)
                   再计算2013年1月1日距离7月的总天数

*1、年数是整百的年(比如说2000年),要能被400整除则是闰年
* (2000/400=5~~2000年是闰年)(1900/400=4.75~~1999年不是闰年)
*2、年数不是整百的年(比如说2007年),要能被4整除则是闰年
* (2068/4=527~~2068年是闰年)(2007/4=501.75~~2007年不是闰年)
*
* 1、3、5、7、8、10、12,31天
* 4、6、9、11,30天
* 2月(闰年29天,平年28天计算)
* 平年一年365天
* 闰年一年366天
*
*  思路:
*  1.先求出两年差
*  2.求出这几天来有多少个闰年
*  3.平年比闰年少一天   这样就可以如此求出:这几年有多少天=(年差*365)+(这几年有多少闰年)
*   
*/
public class Exercise_2 {

public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.print("请输入年:");
  int year = scanner.nextInt();
  System.out.println();
  System.out.print("请输入月:");
  int month = scanner.nextInt();
  System.out.println();
  
  System.out.println(year+"年距离1898年1月1日有"+getDay(1898,year)+"天");
  System.out.println(year+"年"+month+"月1日距离1898年1月1日有"+(getDay(1898,year)+getDay(year,month,0))+"天");
  
}

/**
  * 获取相差年天数
  * @param startYear
  * @param endYear
  * @return
  */
public static int getDay(int startYear ,int endYear){
  int totalDay = 0;
  int totalYear = endYear - startYear;//求年差
  int count4 =  ((totalYear-1)+(4-((startYear)%4)))/4;//求这几年中能被4整除的年份
  int count100 =  ((totalYear-1)+(100-((startYear)%100)))/100;//求这几年中能被100整除的年份
  int count400 =  ((totalYear-1)+(400-((startYear)%400)))/400;//求这几年中能被400整除的年份
  totalDay = ((totalYear) * 365)+(count4-count100+count400);//这几年有多少天=(年差*365)+(这几年有多少闰年)
  return totalDay;
}

/**
  * 获取当前年过了多少天
  * @param year
  * @param motch
  * @param day
  * @return
  */
public static int getDay(int year,int motch,int day){
  int totalDay = 0;
  boolean isLeapYear = ((year%100!=0)&&(year%4==0))||(year%400==0);
  if(motch>=2){
   totalDay = getDay(motch)+day+(isLeapYear?1:0);
  }else{
   totalDay = day;
  }
  return totalDay;
}

/**
  * 获取这个月减一个到今年1月1日的天数(按平年计算)
  * @param motch
  * @return
  */
public static int getDay(int motch){
  int totalDay = 0;
  switch(motch-1){
  case 12:
   totalDay = totalDay+31;
  case 11:
   totalDay = totalDay+30;
  case 10:
   totalDay = totalDay+31;
  case 9:
   totalDay = totalDay+30;
  case 8:
   totalDay = totalDay+31;
  case 7:
   totalDay = totalDay+31;
  case 6:
   totalDay = totalDay+30;
  case 5:
   totalDay = totalDay+31;
  case 4:
   totalDay = totalDay+30;
  case 3:
   totalDay = totalDay+31;
  case 2:
   totalDay = totalDay+28;
  case 1:
   totalDay = totalDay+31;
   default:
    break;
  }
  return totalDay;
}

评分

参与人数 1技术分 +2 收起 理由
神之梦 + 2 赞一个!

查看全部评分

回复 使用道具 举报 1 0
  1. package com.itheima;

  2. import java.util.Calendar;
  3. import java.util.Scanner;

  4. public class CalendarTest {

  5.         /**
  6.          * @param args
  7.          */
  8.         public static void main(String[] args) {
  9.                 /*
  10.                  *1. 版主,我没有用循环的方法,而是直接用了Calendar类的set方法设置了日期,
  11.                  * 同时用getTimeInMillis获取了毫秒值,然后将毫秒值相减的到相差的毫秒数,
  12.                  * 通过转换得到天数,如果此方法有何不妥的地方请指教
  13.                  *2.同时因为不知道输入的格式是什么,那我默认你的输入格式是:年.月
  14.                  */
  15.                
  16.                 //获取输入年月的字符串
  17.                 Scanner s = new Scanner(System.in);
  18.                 System.out.println("请输入您指定的年月,格式:年.月");
  19.                 String inDay = s.next();
  20.                
  21.                 //判断输入的格式和年月是否符合要求,如果是1到9月,直接输入1到9,如果是10到12月,输入两位月份
  22.                 if(!(inDay.matches("[\\d]{4}.[1][12]") || inDay.matches("[\\d]{4}.[1-9]")))
  23.                         throw new RuntimeException("输入的格式不对,请重新输入");
  24.                        
  25.                 //将输入的年月按年和月分开,存进数组
  26.                 String[] date = inDay.split("\\.");
  27. //                System.out.println(date[0]+"::"+date[1]);
  28.        
  29.                 //获取输入的年月
  30.                 int year = Integer.parseInt(date[0].trim());
  31.                 int day = Integer.parseInt(date[1])-1;        //月份有0到11的数字分别表示1到12月
  32. //                System.out.println(year+"::"+day);
  33.                
  34.                 //获取三个Calendar对象,并将c1设置为1898年1月1日,c2设置为输入年的1月1日,c3设置为输入年月的那个月的1日
  35.                 Calendar c1 = Calendar.getInstance();
  36.                 c1.set(1898, 0, 1);
  37.                
  38.                 Calendar c2 = Calendar.getInstance();
  39.                 c2.set(year, 0,1);
  40.                
  41.                 Calendar c3 = Calendar.getInstance();       
  42.                 c3.set(year, day,1);
  43.                
  44.                 long yearMillis = c1.getTimeInMillis();
  45.                 long inYearMillis = c2.getTimeInMillis();               
  46.                 long inMonthMillis = c3.getTimeInMillis();
  47.                
  48.                 //获取输入年到1898年1月1日的天数
  49.                 int yearDays = CalendarTest.getDays((inYearMillis - yearMillis));
  50. //                System.out.println((inYearMillis - yearMillis)/1000/60/60/24);
  51.                
  52.                 //获取输入月到1898年1月1日的天数
  53.                 int monthDays = CalendarTest.getDays((inMonthMillis - yearMillis));
  54. //                System.out.println((inMonthMillis - yearMillis)/1000/60/60/24);
  55.                
  56.                 System.out.println(inDay + ":这一年到1898年1月1日的天数为" + yearDays
  57.                                 + "天,这一月到1898年1月1日的天数为" + monthDays + "天");
  58.                
  59.                                
  60. //                System.out.println(CalendarTest.getDays(24*60*60*1000*2+1000));
  61.         }
  62.         //定义一个通过毫秒值获取天数的方法
  63.         public static int getDays(long timeInMillis){
  64.                 return (int)(timeInMillis/1000/60/60/24);
  65.                
  66.         }

  67. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
神之梦 + 2 考虑很全面,注释明了,不错

查看全部评分

回复 使用道具 举报
今天再干一票
回复 使用道具 举报
看看这个是什么
回复 使用道具 举报
  1. public class Frist {
  2.         public static void main(String[] args)
  3.         {
  4.                 judge(2013,7);
  5.         }
  6.        
  7.         public static void judge(int year,int month)
  8.         {
  9.                 int count = 0; //计数器来统计闰年的个数
  10.                 int yearD = 0;
  11.                 int monthD=0;
  12.                 int[] leap ={0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年时候每月天数,角标0的数值为0这样可以以角标直接对应月份
  13.                 int[] common={0,31,28,31,30,31,30,31,31,30,31,30,31};//平年时每月天数
  14.                 for(int x=1989; x<=year; x++)
  15.                         if(x%4==0 && x%4!=0 ||x%400==0)
  16.                                 count++;
  17.                  yearD=366*count+365*(year-1989-count);
  18.                  
  19.                  System.out.println(year+"年距1989年"+yearD+"天");
  20.                  
  21.                  if(year%4==0 && year%4!=0 ||year%400==0)
  22.                  {
  23.                          for(int x = 0; x<month; x++)
  24.                                  monthD = monthD + leap[x];
  25.                  }
  26.                  else
  27.                  {
  28.                          for(int y = 0; y<month; y++)
  29.                                  monthD = monthD + common[y];
  30.                 }
  31.                        
  32.                  monthD =monthD + yearD;                 
  33.                  System.out.println(year+"年"+month+"月距离1989年1月1日"+monthD+"天");
  34.         }
  35. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 闰年判断错了

查看全部评分

回复 使用道具 举报
本帖最后由 牛牛 于 2013-8-2 12:06 编辑

{:soso_e100:}
回复 使用道具 举报
看题 来了
回复 使用道具 举报
哈哈,看看是什么题目
回复 使用道具 举报
看看 自己有能力做吗
回复 使用道具 举报
为了节省时间就不多代码做更为细致的书写了
  1. import java.util.*;
  2. class SumDay
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 System.out.println("请输入一个的年份yyyy:");//手动输入年月,在这里我就不考虑比1898年小的年了
  7.                 Scanner sc = new Scanner(System.in);
  8.                 int x = sc.nextInt();
  9.                 System.out.println("请输入一个的月份mm:");//为了节省时间在这里就不判断输入是否合法了
  10.                 Scanner sc1 = new Scanner(System.in);
  11.                 int y = sc1.nextInt();
  12.                 int coutDay=0;
  13.                 int[] arr ={31,29,31,30,31,30,31,31,30,31,30,31};//建立平年和闰年的月份数组
  14.                 int[] arr1 ={31,28,31,30,31,30,31,31,30,31,30,31};
  15.                 for (int a=1898;a<x ;a++ )                //循环累加获取距离1898年1月1日的天数
  16.                 {
  17.                         if(getYear(a))
  18.                                 coutDay+=366;
  19.                         else
  20.                                 coutDay+=365;
  21.                 }
  22.                 System.out.println(x+"年距离1898年1月1日"+coutDay+"天");
  23.                 if(getYear(x))                                        //把月份的天数加上
  24.                 {
  25.                         for (int m=0;m<y-1 ;m++ )
  26.                         {
  27.                                 coutDay+=arr[m];
  28.                         }
  29.                 }
  30.                 else
  31.                 {
  32.                         for (int m=0;m<y-1 ;m++ )
  33.                         {
  34.                                 coutDay+=arr1[m];
  35.                         }
  36.                 }
  37.                 System.out.println(x+"年"+y+"月"+"距离1898年1月1日"+coutDay+"天");
  38.         }
  39.         public static boolean getYear(int a)        //建立一个判断闰年和平年的函数
  40.         {
  41.                 if(a%100==0)
  42.                         {
  43.                                 if(a%400==0)
  44.                                         return true;
  45.                                 else
  46.                                         return false;
  47.                         }
  48.                         else if(a%4==0)
  49.                         {
  50.                                 return true;
  51.                         }
  52.                         else
  53.                                 return false;
  54.         }
  55. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 赞一个!

查看全部评分

回复 使用道具 举报
刘张朋 发表于 2013-8-1 19:30
为了做题,老板安排加班都不去了,加油!进黑马

嘿嘿,为理想而努力
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马