`
universsky
  • 浏览: 92217 次
文章分类
社区版块
存档分类
最新评论

输入某年某月某日,判断这一天是这一年的第几天?今天是这一年的第几天? 东海陈光剑

 
阅读更多
/**
 * 
 */
package iSword;

import java.util.Date;
import java.util.Scanner;

/**
 * @author iSword
 *
 */
public class day_of_year {

	/**题目:输入某年某月某日,判断这一天是这一年的第几天?  
	 * @param args
	 */
	public static int[]i_31days=new int[]{1,3,5,7,8,10,12};
	public static int[]i_30days=new int[]{4,6,9,11};
	 
		
	public int is_leap_year(int year){
		
		if (((year%4==0)&(year%100!=0)) || (year%400==0)) return 1;
		else return 0;
		
		
	}
	
	public int is_31days(int month){
		int b=0;
		for (int i=0;i<i_31days.length;i++){
			if (month==i_31days[i]) b=1;
			break;
			}
		
		return b;
		
	}
	
	public int is_30days(int month){
		int b=0;
		for (int i=0;i<i_30days.length;i++){
			if (month==i_30days[i]) b=1;
			break;
			}
		
		return b;
		
	}
	
	public int is_29days(int year){
		day_of_year dy=new day_of_year();
		int b=0;
		if (dy.is_leap_year(year)==1) b=1;
		return b;
		
	}
	
	public int is_28days(int year){
		day_of_year dy=new day_of_year();
		int b=0;
		if (dy.is_leap_year(year)==0) b=1;
		return b;
		
	}
	
	
	public int calculat_day(int year,int month, int day){
      
		int count = 0;
		
		day_of_year dy=new day_of_year();
		//计算30天,31天的月份天数
		for(int m=1;m<month;m++){
			count=count+dy.is_31days(m)*31
						+dy.is_30days(m)*30;					
		}
	 	
		//计算2月份的天数:分leap year and !leap year
 	    count=count+day+dy.is_29days(year)*29+dy.is_28days(year)*28;
 	    return count;
	    //System.out.println(year+"-"+month+"-"+day+"  is the "+count+"th  days of year "+year);
		
	}
	
	 
	
	
	/**
	 * 界面输入日期,计算天数
	 * @return 
	 */
	public void interface_input(){

		int year,month,day;
		day_of_year dy=new day_of_year();
		
		System.out.print("Please input the  (year month day): ");
	    Scanner s = new Scanner(System.in);
	    
	     year  = s.nextInt();
	     month = s.nextInt();
	     day   = s.nextInt();
	
         int count=dy.calculat_day(year, month, day);     

	
	     System.out.println(year+"-"+month+"-"+day+"  is the "+count+"th  days of year "+year);
		
	}
	
	/**
	 * 计算今天是今年的第多少天
	 * author 东海 陈光剑  chenguangjian
	 * Email: universsky@126.com
	 * Blog:  http://blog.sina.com.cn/universsky11
	 *        http://blog.csdn.net/universsky
	 *        
	 */
	@SuppressWarnings("deprecation")
	public void count_today(){
	
		day_of_year dy=new day_of_year();
			
		int t_year,t_month,t_day;
		
		Date dt=new Date();
	
		t_year=dt.getDate();
		
		t_month=dt.getMonth();
		t_day=dt.getDay();

        int count=dy.calculat_day(t_year, t_month, t_day);

		System.out.println("Today is the "+count+"th  days of year "+ (1900+dt.getYear())+"  ["+dt+"]");
		
}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		//day_of_year dy=new day_of_year();
		day_of_year dy=new day_of_year();
		
		dy.interface_input();
		dy.count_today();
		
	}

}


/**
 * 
 */
package iSword;

import java.util.Date;
import java.util.Scanner;


/**
 * 计算今天是今年的第多少天
 * author 东海 陈光剑  chenguangjian
 * Email: universsky@126.com
 * Blog:  http://blog.sina.com.cn/universsky11
 *        http://blog.csdn.net/universsky
 *        
 */

 
public class day_of_year {
 
	 
	
	public static int[]i_31days=new int[]{1,3,5,7,8,10,12};
	public static int[]i_30days=new int[]{4,6,9,11};
	
	
	
	
	public static void main(String[] args) {
		 
		day_of_year dy=new day_of_year();
		
		
		int iput=dy.cal_days(2012, 6, 31);		
		System.out.println(iput);//151
		
		Date date=new Date();
		int cal_today=dy.cal_days(date.getYear(), date.getMonth(),date.getDay());
		System.out.println("Today is the "+cal_today+"th  days of year "+ (1900+date.getYear())+"  ["+date+"]");  

	}
	
	
	 
		
	public int is_leap_year(int year){
		
		if (((year%4==0)&(year%100!=0)) || (year%400==0)) return 1;
		else return 0;
		 
	}
	
	 
	 
	  
	
	
	 
	public int cal_days(int year,int month,int day){
	
		int days = 0,d=0;
		day_of_year dy=new day_of_year();
	
		for (int i=1; i <month; i++) {
			
			////////////////////////////////
			if (i==2){
				   days=28+dy.is_leap_year(year);			 
				}
			
			else{
			
			for (int j=0;j<i_31days.length;j++){	
		      if (i==i_31days[j]) days = 31;
			}
			
			for (int k=0;k<i_30days.length;k++){	
			      if (k==i_30days[k]) days = 30;
				}
			
			}
			d += days;
			
		}
		   //////////////////////////////////////
		   
		 
		
		return d;
	  
}
	
	
	
	

}


Q: 该程序有Bug, 如何白盒测试出来?

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics