Skip to content

Instantly share code, notes, and snippets.

@julia81
Created April 29, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save julia81/ca4aa5b9eeb67dd404be to your computer and use it in GitHub Desktop.
Save julia81/ca4aa5b9eeb67dd404be to your computer and use it in GitHub Desktop.
Age calculator which will tell the user how old they are after the input their age. Programming in Java AgeCalculator.java
import java.util.*;
import javax.swing.*;
public class AgeCalculator
{
public static void main(String[] args)
{
GregorianCalendar now = new GregorianCalendar();
int nowYear; // Current Year "nowYear"
int birthYear; //User inputs their birthYear
int yearsOld; // Stores age as yearsOld
birthYear = Integer.parseInt(JOptionPane.showInputDialog(null,
"In what year were you born?")); //Ask the user to input their birthYear
nowYear=now.get(GregorianCalendar.YEAR);//Finds birthYear in the GregorianCalendar
yearsOld= nowYear - birthYear;//Does the math to find your age
JOptionPane.showMessageDialog(null,
"This is the year you become " + yearsOld + " years old");//Outputs to the user how old they are
}
}
//Julia MacDonald 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment