二月天
This commit is contained in:
parent
c26c27561c
commit
c977d365d8
4
src/chapter5/Example24.java
Normal file
4
src/chapter5/Example24.java
Normal file
@ -0,0 +1,4 @@
|
||||
package chapter5;
|
||||
|
||||
public class Example24 {
|
||||
}
|
4
src/chapter5/Example25.java
Normal file
4
src/chapter5/Example25.java
Normal file
@ -0,0 +1,4 @@
|
||||
package chapter5;
|
||||
|
||||
public class Example25 {
|
||||
}
|
43
src/chapter5/demo55.java
Normal file
43
src/chapter5/demo55.java
Normal file
@ -0,0 +1,43 @@
|
||||
package chapter5;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class demo55 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scn = new Scanner(System.in);
|
||||
System.out.println("请输入要判断的年份:");
|
||||
int year = scn.nextInt();
|
||||
|
||||
System.out.println("方法一:" + year + "的2月有" + getNumOfDaysInFeb1(year) + "天");
|
||||
System.out.println("方法二:" + year + "的2月有" + getNumOfDaysInFeb2(year) + "天");
|
||||
}
|
||||
|
||||
/**
|
||||
* 思路一
|
||||
* @param year 年份
|
||||
* @return 2月的天数
|
||||
*/
|
||||
public static int getNumOfDaysInFeb1(int year) {
|
||||
LocalDate date = LocalDate.of(year, 3, 1);
|
||||
LocalDate feb = date.minusDays(1);
|
||||
return feb.getDayOfMonth();
|
||||
}
|
||||
|
||||
/**
|
||||
* 思路二
|
||||
* @param year 年份
|
||||
* @return 2月的天数
|
||||
*/
|
||||
public static int getNumOfDaysInFeb2(int year) {
|
||||
LocalDate date = LocalDate.of(year, 1, 1);
|
||||
if (date.isLeapYear()) {
|
||||
return 29;
|
||||
} else {
|
||||
return 28;
|
||||
}
|
||||
|
||||
// return date.isLeapYear() ? 29 : 28;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user