Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- import org.joda.time.DateTime;
- public class ageInMonths
- {
- public static void main(String[] args)
- {
- System.out.println("Please enter your birth month.");
- boolean isNumeric = false;
- //array order is: month, year
- int birth[] = new int[2];
- Scanner kb = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
- while (!isNumeric)
- {
- try
- {
- birth[0] = kb.nextInt();
- if (birth[0] == 0)
- {
- throw new Exception();
- }
- isNumeric = true;
- }
- catch (Exception e)
- {
- System.out.println("Please enter in an integer not zero.");
- kb.nextLine();
- }
- }
- isNumeric = false;
- System.out.println("Please enter your birth year.");
- while (!isNumeric)
- {
- try
- {
- birth[1] = kb.nextInt();
- isNumeric = true;
- }
- catch (Exception e)
- {
- System.out.println("Please enter in an integer.");
- kb.nextLine();
- }
- }
- int old[] = new int[2];
- DateTime dt = new DateTime();
- old[0] = dt.getMonthOfYear() - birth[0];
- old[1] = dt.getYear() - birth[1];
- old = conversion(old);
- System.out.println("You are " + (old[0] + old[1]*12) + " months old.");
- }
- static int[] conversion(int[] old)
- {
- if (old[0] < 0)
- {
- old[0] = old[0] + 12;
- --old[1];
- }
- if (old[0] > 11)
- {
- old[0] = old[0] - 12;
- ++old[1];
- }
- return conversion(old);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment