import java.util.Scanner;
public class Lab5{
public static void main(String args[]){
System.out.println("本程式可輸入年份及月份求出當月天數 ");
System.out.print("請輸入年份 ");
Scanner keyin =new Scanner(System.in);
int year = keyin.nextInt();
System.out.print("請輸入月份 ");
int month = keyin.nextInt();
int day = 0;
switch (month){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
day = 31; break;
case 2:
if((year % 4 == 0 && year % 100 != 0) ||
year % 400 == 0){
day = 28;//閏月28天
}else{
day = 29;//非閏月29天
}
break;
case 4:case 6: case 9: case 11:
day = 30; break;
}
System.out.println("西元"+year+" "+month+"月共有"
+day+"天");
}
}
####### source code ends #########################
2009年1月14日 星期三
Java小練習(7)
輸入西元年及月份求當月天數為何?
潤月規則:
A. 年可以被 4 整除但不得同時被 100 整除者
OR
B. 年可以單獨被 400 整除者
輸出結果類似:
西元 2004 年 2 月 => 29天
西元 2005 年 12 月 => 31天
######source code starts ###############
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言