1. 程式人生 > >java——從鍵盤上輸入一個年份,並輸入一個月份(數字),輸出該月份有多少天

java——從鍵盤上輸入一個年份,並輸入一個月份(數字),輸出該月份有多少天

/* (程式頭部註釋開始) </p><p>* 程式的版權和版本宣告部分 
* Copyright (c) 2011, 煙臺大學計算機學院學生                              
* 作    者:   李兆慶                         
* 完成日期:   2012      年   9    月        22   日 
* 輸入描述:   
* 問題描述及輸出:運 用java:從鍵盤上輸入一個年份,並輸入一個月份(數字),輸出該月份有多少天
* 程式頭部的註釋結束 
*/

import javax.swing.JOptionPane;  
public class Num {    
    
    /**  
     * @param args  
     */    
    public static void main(String[] args) {    
        // TODO Auto-generated method stub    
        String str=JOptionPane.showInputDialog("請輸入您要判斷的年份;");  
          
        int y = Integer.parseInt(str);  
          
        System.out.println("您輸入的年份為:"+y);  
        
        String strm = JOptionPane.showInputDialog("請輸入你要判斷的月份為;");
        
        int m = Integer.parseInt(strm);
        
        System.out.println("您輸入的月份為:"+m);
        
         
        if (jud ( y ))   
        {  
            System.out.println(y+"是閏年.");  
        }  
        else   
        {  
            System.out.println(y+"不是閏年");  
        }  
        int d = fig(m);
        
        if (jud(y))
        {
        	if (m == 2)
        	{
        		++d;
        	}
        	System.out.println("您輸入該月份的天數為:"+d);
         }
        else
        {
        	System.out.println("您輸入該月份的天數為:"+d);
        }
        
    	
    }  
            
      
    public static boolean jud (int y)  
    {  
        if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)  
        {  
            return true;  
        }  
        else  
        {  
            return false;  
        }    
    }  
    
    
    
   public static int fig(int m)
   {
   	switch (m)
   	{
   	    case 1:
   	    	return 31;
   	    case 2:
   	    	return 28;
   	    case 3:
   		    return 31;
   	    case 4:
   	    	return 30;
   	    case 5:
   	    	return 31;
   	    case 6:
   	    	return 30;
   	    case 7:
   		    return 31;
   	    case 8:
   	    	return 31;
   	    case 9:
   	    	return 30;
   	    case 10:
   	     	return 31;
   	    case 11:
   	    	return 30;
   	    case 12:
   		    return 31;
   	    default:
   		System.out.println("對不起,您輸入的月份有誤!");
   		return 0;
   	}
   }
}