Results 1 to 4 of 4
Thread: plz help me in my project
- 12-06-2009, 07:08 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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 07:33 PM.
- 12-06-2009, 07:30 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
Repost your code uisng the [CODE] tag please.
- 12-06-2009, 07:31 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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"); } }
- 12-06-2009, 08:21 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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"); }
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
-
Project
By ADARSH in forum New To JavaReplies: 10Last Post: 10-08-2009, 04:27 PM -
open existing project project ..
By itaipee in forum EclipseReplies: 1Last Post: 12-28-2008, 09:12 PM -
Help With Project!!!
By jackhammer in forum New To JavaReplies: 5Last Post: 12-04-2008, 06:10 AM -
Could anyone help with project?
By billdara in forum New To JavaReplies: 1Last Post: 03-12-2008, 06:05 PM
Bookmarks