编写一个方法,计算你出生的那一天是星期几?并显示距离现在几天,用Scanner和
时间: 2023-12-03 09:43:00 浏览: 93
Calendar类实现。
可以参考以下代码:
```java
import java.util.Calendar;
import java.util.Scanner;
public class Birthday {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的出生年份:");
int year = scanner.nextInt();
System.out.println("请输入你的出生月份:");
int month = scanner.nextInt() - 1; // Calendar中月份从0开始
System.out.println("请输入你的出生日期:");
int day = scanner.nextInt();
Calendar birthday = Calendar.getInstance();
birthday.set(year, month, day);
int dayOfWeek = birthday.get(Calendar.DAY_OF_WEEK);
String[] daysOfWeek = {"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
System.out.println("你的生日是:" + daysOfWeek[dayOfWeek]);
Calendar now = Calendar.getInstance();
long days = (now.getTimeInMillis() - birthday.getTimeInMillis()) / (24 * 60 * 60 * 1000);
System.out.println("距离现在已经过去了 " + days + " 天。");
}
}
```
运行程序后,按照提示输入出生年月日即可看到结果。
阅读全文