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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 空心祭 中级黑马   /  2014-3-19 17:14  /  1198 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

问题:获取某一年中有多少个周六日
public static int getCount(int year)
        {
                int count = 0;
                int month = 0;
                int day = 1;
                Calendar c = Calendar.getInstance();
                c.set(year,month,day);
                w:while (month>=0&&month<=11)
                {
                        n:while((c.get(Calendar.DAY_OF_WEEK))==1||(c.get(Calendar.DAY_OF_WEEK))==7)
                        {
                                count++;
                                day++;
                                if (day > c.getActualMaximun(month))//返回给定时间可能出现的最大值
                                {
                                        month++;
                                        continue w;
                                }
                        }
                }
                return count;

        }
这是我写的方法  为什么编译不通过,不知道问题出哪里了

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

5 个回复

倒序浏览
你的全部代码呢??
回复 使用道具 举报
import java.util.*;
是不是忘记导入util包了,你试试看看
回复 使用道具 举报
c.getActualMaximun(month) 方法报错了 c.getActualMaximum(month)
   n:  没有调用到   你代码没发完吧?
你改了这里再导入java.util包 试试。
回复 使用道具 举报
楼主这是我根据你思路做的,
  1. import java.util.Calendar;
  2. public class GetCalendar {
  3.         public static void main(String[] args) {
  4.                 // TODO Auto-generated method stub
  5.                 try{
  6.                 System.out.println(getCount(2012));
  7.                 }catch(Exception e){
  8.                         e.printStackTrace();
  9.                 }
  10.         }

  11.         public static String getCount(int year)
  12.     {
  13.             int isSatSum=0;//星期六总天数
  14.             int isSunSum=0;//星期天总天数
  15.             Calendar c = Calendar.getInstance();
  16.             c.set(year,0,1);//设置当前年从1月1日开始
  17.             int currentMonth=c.get(Calendar.MONTH);//获取当前月份默认1月
  18.             while (currentMonth<12)
  19.             {
  20.                     int day=1;
  21.                     System.out.println("当前是:"+(currentMonth+1)+"月,共"+c.getActualMaximum(Calendar.DAY_OF_MONTH)+"天");//每月有多少天
  22.                     while(day<c.getActualMaximum(Calendar.DAY_OF_MONTH))//判断当前天数是否小于本月总天数
  23.                     {
  24.                             if((c.get(Calendar.DAY_OF_WEEK))==7){
  25.                                     System.out.println("今天是星期六"+c.get(Calendar.DATE)+"号");//获取周末是几号
  26.                                     isSatSum++;
  27.                             }else if((c.get(Calendar.DAY_OF_WEEK))==1){
  28.                                     System.out.println("今天是星期日"+c.get(Calendar.DATE)+"号");
  29.                                     isSunSum++;
  30.                             }                             
  31.                         day++;            
  32.                         c.set(year,currentMonth,c.get(Calendar.DATE)+1);      // 重新设置当前是第几天      
  33.                     }
  34.                     c.set(year,currentMonth+1,1); //重新设置当前月份
  35.                     currentMonth+=1;   
  36.             }
  37.             return "当前年有周六:"+isSatSum+"天,周末:"+isSunSum+"天,共计双休:"+(isSatSum+isSunSum)+"天";
  38.     }   
  39. }
复制代码
那看下,还有什么不懂再说。

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
应该是你的方法名写错了:在这里的一个方法:
if (day > c.getActualMaximun(month))//返回给定时间可能出现的最大值
应该是:getActualMaximum(month);

个人拙见,仅供参考。

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

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