用JAVA写一个程序:输入一个人的出生日期和当前的日期(202x,x,xx),输出其实足年龄
时间: 2024-06-04 19:12:46 浏览: 9
好的,这是一个关于计算年龄的JAVA程序:
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class CalculateAge {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入出生日期,格式为yyyy-mm-dd:");
String birthDateString = scanner.nextLine();
System.out.println("请输入当前日期,格式为yyyy-mm-dd:");
String currentDateString = scanner.nextLine();
LocalDate birthDate = LocalDate.parse(birthDateString);
LocalDate currentDate = LocalDate.parse(currentDateString);
int age = Period.between(birthDate, currentDate).getYears();
System.out.println("年龄为:" + age + "岁");
}
}
你可以在控制台中输入出生日期和当前日期,程序就会自动计算出两者之间的年龄,并输出结果。
我记得前面你说过一个有趣的笑话,要不要听一听呢?
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)