Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default plz help me in my project

    Hello
    i wrote this program to find the age and when my teacher saw it he said it's true but you have to add 1 (if) for each month that's mean 12 (if) and put in your maind the year that has 28 days in Feb. I don't understand why we need 12(if) and how can i do this program plz help me i need it during this week to submit it ,,,


    //this programe will find your age
    import java.util.*;
    public class Age1{
    public static void main(String[] args){
    int x,y,z,s,m,k,a,d,r;
    String name;
    Scanner scan=new Scanner(System.in);
    System.out.println("Enter your name");
    name=scan.nextLine();
    do{
    System.out.println("Enter the day of the month your born on");
    x=scan.nextInt();
    if(x<=0||x>31){
    System.out.println("oobs there are no date like this");
    }
    }while(x<=0||x>31);
    do{
    System.out.println("Enter the month you born in");
    y=scan.nextInt();
    if(y<=0||y>31){
    System.out.println("oobs there are no date like this");
    }
    }while(y<=0||y>31);
    do{
    System.out.println("Enter the year you born in");
    z=scan.nextInt();
    if(z<=0||z>2011){
    System.out.println("oobs there are no date like this");
    }
    }while(z<=0||z>2011);
    do{
    System.out.println("Enter the date of today");
    s=scan.nextInt();
    if(s<=0||s>31){
    System.out.println("oobs there are no date like this");
    }
    }while(s<=0||s>31);
    do{
    System.out.println("Enter this month");
    m=scan.nextInt();
    if(m<=0||m>31){
    System.out.println("oobs there are no date like this");
    }
    }while(m<=0||m>31);
    do{
    System.out.println("Enter this year");
    k=scan.nextInt();
    if(k<=0||k>2011){
    System.out.println("You are not born right now :)");
    }
    }while(k<=0||k>2011);
    r=s-x;
    if(r<0){
    r=x-s;
    }
    a=m-y;
    if(a<0){
    a=y-m;
    }
    d=k-z;
    System.out.println("Dear "+name+" your age is "+d+" year\n and "+a+" month \n and "+r+" day");
    }
    }
    Last edited by smart princess; 12-06-2009 at 06:33 PM.

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Repost your code uisng the [CODE] tag please.

  3. #3
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by sky View Post
    Repost your code uisng the [CODE] tag please.
    ok

    Java Code:
    //this programe will find your age
    import java.util.*;
    public class Age1{
      public static void main(String[] args){
        int x,y,z,s,m,k,a,d,r;
       String name;
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter your name");
        name=scan.nextLine();
          do{
        System.out.println("Enter the day of the month your born on");
        x=scan.nextInt();
        if(x<=0||x>31){
          System.out.println("oobs there are no date like this");
        }
          }while(x<=0||x>31);
          do{
        System.out.println("Enter the month you born in");
        y=scan.nextInt();
        if(y<=0||y>31){
          System.out.println("oobs there are no date like this");
        }
          }while(y<=0||y>31);
          do{
        System.out.println("Enter the year you born in");
        z=scan.nextInt();
        if(z<=0||z>2011){
          System.out.println("oobs there are no date like this");
        }
          }while(z<=0||z>2011);
          do{
        System.out.println("Enter the date of today");
        s=scan.nextInt();
        if(s<=0||s>31){
          System.out.println("oobs there are no date like this");
        }
          }while(s<=0||s>31);
          do{
        System.out.println("Enter this month");
        m=scan.nextInt();
        if(m<=0||m>31){
          System.out.println("oobs there are no date like this");
        }
          }while(m<=0||m>31);
          do{
        System.out.println("Enter this year");
        k=scan.nextInt();
        if(k<=0||k>2011){
          System.out.println("You are not born right now :)");
        }
        }while(k<=0||k>2011);
         r=s-x;
        if(r<0){
          r=x-s;
        }
        a=m-y;
        if(a<0){
          a=y-m;
        }
        d=k-z;
        System.out.println("Dear "+name+" your age is "+d+" year\n and "+a+" month \n and "+r+" day");
      }
    }

  4. #4
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    First of all. You should give your variables names like "days" "months", etc. that way the code is much more readable. Second: When you do the substraction of the months and years, you can just take the absolute value instead of use an if. And finally, I think that what the teacher wanted to say is that when you read the days from the user:

    Java Code:
    if(x<=0||x>31){
          System.out.println("oobs there are no date like this");
        }
    That is a bad error control, because there are months with 30 days, months with 31 days and then February that can has 29 on the leap years. To know if a year is a leap year, I found the following algorithm in the Wikipedia:
    Java Code:
    if year modulo 400 is 0 then leap
     else if year modulo 100 is 0 then no_leap
     else if year modulo 4 is 0 then leap
     else no_leap

Similar Threads

  1. Project
    By ADARSH in forum New To Java
    Replies: 10
    Last Post: 10-08-2009, 03:27 PM
  2. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 08:12 PM
  3. Help With Project!!!
    By jackhammer in forum New To Java
    Replies: 5
    Last Post: 12-04-2008, 05:10 AM
  4. Could anyone help with project?
    By billdara in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •