- import java.util.Scanner;
- public class GetDays {
- public static void main(String[] args) {
- int[] months = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int year, month, day, days = 0;
- Scanner scanner = new Scanner(System.in);
- while (true) {
- System.out.print("请输入年份");
- year = scanner.nextInt();
- if (1 <= year) {
- break;
- }
- System.out.println("输入无效");
- }
- while (true) {
- System.out.print("请输入月份");
- month = scanner.nextInt();
- if (1 <= month & 12 >= month) {
- break;
- }
- System.out.println("输入无效");
- }
- while (true) {
- System.out.print("请输入日子");
- day = scanner.nextInt();
- if (2 == month) {
- if (1 > day ? false : isRunNian(year) ? 29 >= day : 28 >= day) {
- break;
- }
- } else if (1 <= day & day <= months[month]) {
- break;
- }
- System.out.println("输入无效");
- }
- scanner.close();
- for (int i = 1; i < month; i++) {
- days += months[i];
- }
- days += day;
- if (isRunNian(year) & 2 < month) {
- days++;
- }
- System.out.println("第" + days + "天");
- }
- public static boolean isRunNian(int year) {
- return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
- }
- }
复制代码 |