Java平閏年判斷的方法總結(jié)
更新時間:2018年02月24日 11:13:12 作者:彬菌
本篇文章給大家整理了Java平閏年判斷的兩種方法,大家在寫程序的時候如果用的到參考下吧。
普通方法:
import java.util.Scanner; public class Bissextile{ public static void main(String[] args){ Scanner input=new Scanner(System.in);//聲明掃描儀變量 System.out.println("請輸入年份");//系統(tǒng)提示輸入年份 try{ //監(jiān)聽異常 while(true){ //不斷讀取用戶輸入的值 int years=input.nextInt();//取得下一行輸入的年份值 if (years<1000||years>9999) System.out.println("請輸入大于1000小于9999的年份"); else if(years % 4 == 0 && years % 100 != 0 || years % 400 == 0){ //平閏年判斷算法 System.out.println(years+"年是閏年"); } else { System.out.println(years+"年是平年"); } } } catch(Exception e){ //異常處理 System.out.println("請正確輸入"); e.printStackTrace(); //打印異常信息在程序中出錯的位置及原因 } } }
一般函數(shù)/方法:
import java.util.Scanner; public class Bissextile { boolean bissextile(int year){ //創(chuàng)建boolean類型的方法 if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){ //平閏年判斷算法 return true; } else{ return false; } } public static void main(String[] args){ Bissextile b=new Bissextile(); //創(chuàng)建對象 Scanner input=new Scanner(System.in);//聲明掃描儀變量 System.out.println("請輸入年份");//系統(tǒng)提示輸入年份 try{ while(true){ //不斷讀取用戶輸入的值 int year1=input.nextInt();//取得下一行輸入的年份值 if (year1<1000||year1>9999){ System.out.println("請輸入大于1000小于9999的年份"); } else if(b.bissextile(year1)){ //對象調(diào)用bissextile方法 System.out.println(year1+"是閏年"); } else{ System.out.println(year1+"是平年"); } } } catch(Exception e){ //異常處理 System.out.println("請正確輸入"); e.printStackTrace(); //打印異常信息在程序中出錯的位置及原因 } } }
注解:第二種方法用到了面向?qū)ο蟮乃枷?/p>
相關(guān)文章
SpringBoot整合screw實現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼
這篇文章主要介紹了SpringBoot整合screw實現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09springboot中用fastjson處理返回值為null的屬性值
在本篇文章里小編給大家整理的是一篇關(guān)于springboot中用fastjson處理返回值問題詳解內(nèi)容,需要的朋友們參考下。2020-03-03idea中使用maven?archetype新建項目時卡住問題解決方案
這篇文章主要介紹了idea中使用maven?archetype新建項目時卡住,解決本問題的方法,就是在maven的runner加上參數(shù)-DarchetypeCatalog=local就可以了,不需要下載xml文件再放到指定目錄,需要的朋友可以參考下2023-08-08