Использование Try-Catch-Наконец - PullRequest
0 голосов
/ 06 апреля 2020

Ниже приведен мой код для пользователя, который вводит текущую дату и дату своего рождения. Затем распечатать, сколько им лет. Я пытаюсь использовать структуру try, catch или finally для обработки всех ошибок и / или исключений (символы, используемые вместо int, отрицательные числа и т. Д. c ...) и пытаюсь выяснить, как добавить расширение високосного года. чтобы быть более точным.

Я не хочу, чтобы кто-нибудь писал код, просто дайте мне совет специалиста, это всегда полезно. Еще раз спасибо!

import static java.lang.System.exit;
import java.util.Scanner;

public class BirthCalc {
  static void checkAgeFormat(int current_date, int current_month,
    int current_year, int birth_date,
    int birth_month, int birth_year) {
    int f = 0;
    if (current_date <= 1 || current_date >= 31) {
      System.out.println("Invalid Today's DD");
      f = 1;
    }
    if (current_month <= 1 || current_month >= 12) {
      System.out.println("Invalid Today's MM");
      f = 1;
    }
    if (current_year < 2020 || current_year > 2020) {
      System.out.println("Invalid Today's YYYY");
      f = 1;
    }
    if (birth_date <= 1 || birth_date >= 31) {
      System.out.println("Invalid Birthday DD");
      f = 1;
    }
    if (birth_month <= 1 || birth_month >= 12) {
      System.out.println("Invalid Birthday MM");
      f = 1;
    }
    if (birth_year < 1910 || birth_year > 2020) {
      System.out.println("Invalid Birthday YYYY");
      f = 1;
    }
    if (f == 1) {
      exit(0);
    }
  }
  static void displayDate(int current_date, int current_month,
    int current_year, int birth_date,
    int birth_month, int birth_year) {
    System.out.println("Current date");
    System.out.println("Years: " + current_year +
      " Months: " + current_month + " Days: " +
      current_date);
    System.out.println("Birth date");
    System.out.println("Years: " + birth_year +
      " Months: " + birth_month + " Days: " +
      birth_date);
  }
  static void findAge(int current_date, int current_month,
    int current_year, int birth_date,
    int birth_month, int birth_year) {
    int month[] = {
      31,
      28,
      31,
      30,
      31,
      30,
      31,
      31,
      30,
      31,
      30,
      31
    };

    if (birth_date > current_date) {
      current_month = current_month - 1;
      current_date = current_date + month[birth_month - 1];
    }


    if (birth_month > current_month) {
      current_year = current_year - 1;
      current_month = current_month + 12;
    }

    int calculated_date = current_date - birth_date;
    int calculated_month = current_month - birth_month;
    int calculated_year = current_year - birth_year;
    if (calculated_year > 110) {
      System.out.println("Error: Age Exceeded 110 Years; Please Re-Enter Birth Year");
    }

    System.out.println("Present Age");
    System.out.println("Years: " + calculated_year +
      " Months: " + calculated_month + " Days: " +
      calculated_date);
  }
  public static void main(String[] args) {
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter Today's DD ");
    int current_date = s1.nextInt();
    System.out.println("Enter Today's MM ");
    int current_month = s1.nextInt();
    System.out.println("Enter Today's YYYY ");
    int current_year = s1.nextInt();

    System.out.println("Enter Birthday DD");
    int birth_date = s1.nextInt();
    System.out.println("Enter Birthday MM");
    int birth_month = s1.nextInt();
    System.out.println("Enter Birthday YYYY ");
    int birth_year = s1.nextInt();
    checkAgeFormat(current_date, current_month, current_year,
      birth_date, birth_month, birth_year);


    findAge(current_date, current_month, current_year,
      birth_date, birth_month, birth_year);
  }
}
...