使用 Try-Catch-Finally

huangapple 未分类评论46阅读模式
英文:

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 &lt;= 1 || current_date &gt;= 31) {
      System.out.println(&quot;Invalid Today&#39;s DD&quot;);
      f = 1;
    }
    if (current_month &lt;= 1 || current_month &gt;= 12) {
      System.out.println(&quot;Invalid Today&#39;s MM&quot;);
      f = 1;
    }
    if (current_year &lt; 2020 || current_year &gt; 2020) {
      System.out.println(&quot;Invalid Today&#39;s YYYY&quot;);
      f = 1;
    }
    if (birth_date &lt;= 1 || birth_date &gt;= 31) {
      System.out.println(&quot;Invalid Birthday DD&quot;);
      f = 1;
    }
    if (birth_month &lt;= 1 || birth_month &gt;= 12) {
      System.out.println(&quot;Invalid Birthday MM&quot;);
      f = 1;
    }
    if (birth_year &lt; 1910 || birth_year &gt; 2020) {
      System.out.println(&quot;Invalid Birthday YYYY&quot;);
      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(&quot;Current date&quot;);
    System.out.println(&quot;Years: &quot; + current_year +
      &quot; Months: &quot; + current_month + &quot; Days: &quot; +
      current_date);
    System.out.println(&quot;Birth date&quot;);
    System.out.println(&quot;Years: &quot; + birth_year +
      &quot; Months: &quot; + birth_month + &quot; Days: &quot; +
      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 &gt; current_date) {
      current_month = current_month - 1;
      current_date = current_date + month[birth_month - 1];
    }


    if (birth_month &gt; 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 &gt; 110) {
      System.out.println(&quot;Error: Age Exceeded 110 Years; Please Re-Enter Birth Year&quot;);
    }

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

    System.out.println(&quot;Enter Birthday DD&quot;);
    int birth_date = s1.nextInt();
    System.out.println(&quot;Enter Birthday MM&quot;);
    int birth_month = s1.nextInt();
    System.out.println(&quot;Enter Birthday YYYY &quot;);
    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 -->

huangapple
  • 本文由 发表于 2020年4月7日 00:17:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/61064231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定