英文:
Using Try-Catch-Finally
问题
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);
}
}
英文:
Below is my code for a User inputting the current date and their birthdate. Then printing out how old they are. I am trying to use try, catch or finally block structure to handle all errors and/or exceptions (characters used instead of int, negative numbers, etc...) and trying to figure out how to add a leap year extension to be more precise.
I don't want anyone to write the code, just give me some expert advice, it is always helpful. Thanks again!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
}
}
<!-- end snippet -->
专注分享java语言的经验与见解,让所有开发者获益!
评论