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)文章
Java?數(shù)據(jù)交換?Json?和?異步請求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,采用鍵值對的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲,這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請求Ajax,需要的朋友可以參考下2023-09-09
Spring Cloud Gateway自定義異常處理Exception Handler的方法小結(jié)
這篇文章主要介紹了Spring Cloud Gateway自定義異常處理Exception Handler的方法,本文通過兩種方法結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Java常用數(shù)字工具類 數(shù)字轉(zhuǎn)漢字(1)
這篇文章主要為大家詳細介紹了Java常用數(shù)字工具類,數(shù)字轉(zhuǎn)漢字,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
java編程SpringSecurity入門原理及應(yīng)用簡介
Spring 是非常流行和成功的 Java 應(yīng)用開發(fā)框架,Spring Security 正是 Spring 家族中的成員。Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案2021-09-09
SpringCloud?Gateway詳細分析實現(xiàn)負載均衡與熔斷和限流
這篇文章主要介紹了SpringCloud?Gateway實現(xiàn)路由轉(zhuǎn)發(fā),負載均衡,熔斷和限流,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例
本篇文章主要介紹了SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例,可以限制登陸次數(shù),有興趣的同學(xué)可以了解一下。2017-03-03

