plz help me in my project

• 12-06-2009, 07:08 PM
smart princess
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);
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");
}
}
• 12-06-2009, 07:30 PM
sky
• 12-06-2009, 07:31 PM
smart princess
Quote:

Originally Posted by sky

ok

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");   } }```
• 12-06-2009, 08:21 PM
sky
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:

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:
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```