應為要有交互,選擇了Js來實現,也算是 結對編程 的初試吧。 我將顯示部分用html 寫好,點擊的按鈕觸發事件函數是check();
// 輸入值判斷部分
...
//調用核心函數
days(theYear,theMonth,theDay);
}
核心函數days如下:
var date0 = new Date(year,0,1); //當年的第一天是周幾
// alert(date0.getDay());
var date1 = new Date(year,month-1,day); //將日期值格式化,0-11代表1月-12月;
// alert((days + date0.getDay()+6)/7);
var nthOfWeek = Math.floor((days + date0.getDay()+6)/7); //向下取整
// alert(nthOfWeek);
var toDay = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
//day.getDay();根據Date返一個星期中的某其中0為星期日
alert("該日期是一年中的第"+days+"天/n"+" 是第"+nthOfWeek+"周的"+toDay[date1.getDay()]);
}
調試過程中遇到了許多意外的錯誤,如類型的不匹配帶來的計算錯誤,如數字的舍入問題;
在隊友的協助下,他負責審核和協助抓蟲子,我負責實施和編碼;
在最后一個環節,對輸入值的測試中,我們很好的相互協助,分析不同的輸入情況,涵蓋了各種可能的意外,很快的完成了功能的完善;
下面是對輸入值的判斷是否允許的代碼 :
if((theMonth == 2 && theDay > 29 && isLeapYear(theYear))||(theMonth == 2 && theDay > 28 && !isLeapYear(theYear))) {
alert("輸入有誤,請重新輸入");
return ;
}
if((theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) && theDay == 31 ) {
alert("輸入有誤,請重新輸入");
return ;
}
新聞熱點
疑難解答