1. 程式人生 > >7. 分三次輸入年,月,日,判斷改日是該年的第幾天 →(if判斷、switch...case結構方法)

7. 分三次輸入年,月,日,判斷改日是該年的第幾天 →(if判斷、switch...case結構方法)

分三次輸入年.月.日,判斷改日是該年的第幾天  →(if判斷、switch...case結構方法)

首先,我們用3.if...else if...else... 來解析這道題:
            語法:if(條件1){語句塊1;}    else if(條件2){語句塊2;}    else if(條件2){語句塊2;}    else{語句塊n;}
<!doctype html>
<html lang="en">
 <head>
	 <meta charset="UTF-8">
     <title>Document</title>
	 <link rel="stylesheet" style="text/css" href="">
	 <style> </style>
 </head>
 <body>
	 <!doctype html>
<html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>if分支</title>
	 <link rel="stylesheet" style="text/css" href="">
     <style></style>
	 <script></script>
 </head>
 <body>
	<script>
		// 分三次輸入年,月,日,判斷改日是該年的第幾天
		// 1 3 5 7 8 10 12月(各31天)
		// 4,6,9,11月(30天)
		// 2月(平年28,閏年29)
		// 2017,9,20第幾天? (累加1-8月,在加20)


		// 1、分三次輸入年(year)月(month)日(day)
		var year = Number(prompt("請輸入年"));
		var month = Number(prompt("請輸入月"));
		var day = Number(prompt("請輸入天"));
		// 2、判斷year是否為閏年,並將結果儲存在 isRun中
		var isRun = (year%4==0) && (year%100!=0) || (year%400==0);
		// if判斷month到底是幾月,將月份進行累加,將結果儲存到totalDay中;
		var totalDay=0;
		if(month==1){
			totalDay = day;
		}else if(month==2){
			totalDay = 31+day;
		}else if(month==3){
			totalDay = 31+(isRun ? 29 :28)+day;
		}else if(mouth==4){
			totalDay = 31+(isRun ? 29 :28)+31+day;
		}else if(month==5){
			totalDay = 31+(isRun ? 29 :28)+31+30+day;
		}else if(month==6){
			totalDay = 31+(isRun ? 29 :28)+31+30+31+day;
		}else if(month==7){
			totalDay = 31+(isRun ? 29 :28)+31+30+31+30+day;
		}else if(month==8){
			totalDay = 31+(isRun ? 29 :28)+31+30+31+30+31+day;
		}else if(month==9){
			totalDay = 31+(isRun ? 29 :28)+31+30+31+30+31+30+day;
		}else if(month==10){			
			totalDay = 31+(isRun ? 29 :28)+31+30+31+30+31+30+30+day;
		}
		//.......month一直到12;
		console.log(totalDay);
		</script>
	<button onclick="totalDay()">計算年的天數</button>
 </body>
</html>

其次,我們用4.switch...case來解析這道題:
    .作用:等值判斷
      注意:switch後的值或者表示式,與case的值進行比較時是使用的===判斷
           語法:switch(值/表示式){ case值1: 語句塊1; break;(結束switch結構,可選的) ...... default: break;}
          特殊用法:switch(值/表示式){ case 值1: case 值2: case 值3: 語句塊;}

<!doctype html>
<html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>if分支</title>
	 <link rel="stylesheet" style="text/css" href="">
     <style></style>
	 <script></script>
 </head>
 <body>
	<script>
	function colday(){
			var year = Number(prompt("請輸入年"));
			var month = Number(prompt("請輸入月"));
			var day = Number(prompt("請輸入天"));
			var totalDay=0;
			switch(month-1){
				case 11:
					totalDay=totalDay+30;
				case 10:
					totalDay+=31;
				case 9:
					totalDay+=30;
				case 8:
					totalDay+=31;
				case 7:
					totalDay+=31;
				case 6:
					totalDay+=30;
				case 5:
					totalDay+=31;
				case 4:
					totalDay+=30;
				case 3:
					totalDay+=31;
				case 2:
					totalDay+=28;
					if((year%4==0) && (year%100!=0) || (year%400==0)){
						totalDay+=1;	
					}
				case 1:
					totalDay+=31;
			}
			totalDay+=day;
			console.log(year+"年"+month+"月"+day+"日是"+year+"年的第"+totalDay+"天");
		}
	</script>
	<button onclick="colday()">計算年的天數</button>
 </body>
</html>