我做了一个年龄计算器,但它显示负值

时间:2017-02-24 00:03:42

标签: java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.awt.*;
import hsa.Console;

public class AgeCalculator
{
    static Console c;

    public static void main (String[] args)
    {
        c = new Console ();
        //Variable Declaration
        int days;
        int month;
        int year;

        days = 0;
        month = 0;
        year = 0;

        //I used Ready To Program IDE
        c.println ("This program calculates your age to the exact date");

        c.println ();

        c.println ("Please answer in numerical format day/month/year (Eg: 1/1/2016)");

        c.println ();


        //Input Section
        c.print ("Please enter the day you were born in: ");
        days = c.readInt ();
        c.println ();

        c.print ("Please enter the month you were born in: ");
        month = c.readInt ();
        c.println ();

        c.print ("Please enter the year you were born in: ");
        year = c.readInt ();
        c.clear();

        //Processing Section
        // This is where the calculations are happening and I believe this is where I am making a mistake
        c.println ("Date of Birth: " + days + "/" + month + "/" + year);
        Date d = new Date ();
        c.println ("Current Date: " + d.getDate () + "/" + (d.getMonth () + 1) + "/" + (d.getYear () + 1900));

        days = d.getDate () + 30 - days;
        month = d.getMonth () + 1 - month;
        year = d.getYear () + 1900 - year;

        //Output Section
        c.println ("Age:" + year + " years " + month + " months " + days + " days "); //Displays age in years,month, and days


    }
}

0 个答案:

没有答案