Results 1 to 20 of 25
Thread: Seconds Left to Live
- 10-02-2011, 08:37 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Seconds Left to Live
My program is giving me the wrong answers. The programs job is the ask the user for the years, months, and days they have been alive. Then calculate how many seconds the user has been alive, and then how many seconds the user has left to live (by subtracting the average 2.5 billion average of seconds. I have already calculated all the if statements and looked over my calculations numerous times, so I don't see what it is not giving me the right output. Thanks for your help.
monthsToDays = years * 365;
monthsToDays = (monthsToDays + months);
monthsToDays = (monthsToDays + days);
secleft = monthsToDays * 24 * 60 * 60;
System.out.println("You have been alive for " + secleft + " seconds");
System.out.println("The average human life is 2,500,000,000 seconds");
System.out.printf("You have " + (averagelife - secleft) + " seconds to live !");
- 10-03-2011, 12:46 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Seconds Left to Live
Can your post what your output is?
To add variables together you need to use: '+=' and when subtracting: '-='.monthsToDays = years * 365;
monthsToDays = (monthsToDays + months);
monthsToDays = (monthsToDays + days);
Try changing your calculations to this:
Java Code:monthsToDays = years * 365; monthsToDays = monthsToDays += months; monthsToDays = monthsToDays += days;
- 10-03-2011, 12:52 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
That is correct but += is just a shortcut to what he's doing, his way works as well.
I see one objective here is to convert everything you get from the user (years, months, and days) to days and then add that together.
Take a look atDo you see anything wrong with it relating to your objective?Java Code:monthsToDays = (monthsToDays + months);
Last edited by Solarsonic; 10-03-2011 at 12:59 AM.
- 10-03-2011, 01:21 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
- 10-03-2011, 01:30 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
Should it be like this instead?
secleft = years * 365 += monthstoDays +=days;
secleft = monthsToDays * 24 * 60 * 60;
System.out.println("You have been alive for " + secleft + " seconds");
System.out.println("The average human life is 2,500,000,000 seconds");
System.out.printf("You have " + (averagelife - secleft) + " seconds to live !");
- 10-03-2011, 01:33 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
Last edited by Solarsonic; 10-03-2011 at 01:35 AM.
- 10-03-2011, 01:46 AM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
Your code is illogical and flawed in many ways.
I advise you to re-try this from the beginning, using my code:
for obtaining seconds lived.Java Code:int totalDaysLived; totalDaysLived += years * 365; totalDaysLived += months * 30; totalDaysLived += days; int secondsLived = totalDaysLived * 24 * 60 * 60;
- 10-03-2011, 02:01 AM #8
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
Change
toJava Code:Long totaldayslived = null;
Also, I see what you're trying to do with your if statements.Java Code:long totaldayslived;
You can figure out the exact number of days in each month but for that you'll need to get today's date etc; your if statements are flawed.
If you're new to Java then I'm guessing your instructor expects you to just estimate 30 days per month, otherwise you'll have to work with the JDK Date class (http://download.oracle.com/javase/1....util/Date.html) and fix your if statements.
- 10-03-2011, 02:06 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
When I changed it to this...
Long totaldayslived;
totaldayslived += years * 365;
// On the above totaldayslived it says the local variable totaldayslived may not have been initialized.
totaldayslived += months * 30;
totaldayslived += days;
Also, we are supposed to assume the different days in different months. I am not familiar with the with the Date Class you are referring to.
- 10-03-2011, 02:11 AM #10
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
Well you can only initialize a variable like
inside of a static method or outside any methods.Java Code:long totaldayslived;
Change it to
As for the different days in different months, you won't know which months they've lived unless you know today's date correct?Java Code:long totaldayslived - 0L;
If today was December 7th, you would get different months that he lived than if it was October 2nd.Last edited by Solarsonic; 10-03-2011 at 02:15 AM.
- 10-03-2011, 02:16 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
These are part of the instructions given...
Write a Java application program that will ask the user for their age in years, months, and days. The program will then calculate and display the user's age in seconds.
Begin by asking the user to enter their age in years, months and days.
After the age has been entered, calculate the number of seconds the user has been alive.
ect..
- 10-03-2011, 02:22 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
More instructions..
You must, however, correctly take into account the different number of days in different months. For example, if the user enters 5 for the number of months, you would add up the number of days in the first 5 months: 31+28+31+30+31
- 10-03-2011, 02:25 AM #13
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
I am a beginner, but we are supposed to take into account the different days in different months, but can ignore leap years.
"You must, however, correctly take into account the different number of days in different months. For example, if the user enters 5 for the number of months, you would add up the number of days in the first 5 months: 31+28+31+30+31"
- 10-03-2011, 02:28 AM #14
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 10-03-2011, 02:29 AM #15
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
Yes I changed that. Thank you. Still so confused about this though. I just don't get why its not giving out the correct output. Been looking at it for hours! Yikes!
- 10-03-2011, 02:30 AM #16
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
Also I see that you can just erase
completely, since you already added your months to the days in your if statements. That could be why you're getting the wrong answers.Java Code:totaldayslived += months * 30;
Btw next time please post all of your code because I could have spotted this earlier on.Last edited by Solarsonic; 10-03-2011 at 02:37 AM.
- 10-03-2011, 02:58 AM #17
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
Change
toJava Code:if (months ==1) { monthstodays = (31L); } else if (months ==2) { monthstodays = (59L); } else if (months ==3) { monthstodays = (90L); } else if (months ==4) { monthstodays = (120L); } else if (months ==5) { monthstodays = (151L); } else if (months ==6) { monthstodays = (181L); } if (months ==7) { monthstodays = (212L); } if (months ==8) { monthstodays = (243L); } if (months ==9) { monthstodays = (273L); } if (months == 10) { monthstodays = (304L); } if (months ==11) { monthstodays = (334L); } if (months ==12) { monthstodays = (365L); }
Your program will give you the correct output.Java Code:if (months ==1) { days += (31L); } else if (months ==2) { days += (59L); } else if (months ==3) { days += (90L); } else if (months ==4) { days += (120L); } else if (months ==5) { days += (151L); } else if (months ==6) { days += (181L); } if (months ==7) { days += (212L); } if (months ==8) { days += (243L); } if (months ==9) { days += (273L); } if (months == 10) { days += (304L); } if (months ==11) { days += (334L); } if (months ==12) { days += (365L); }
Also I don't know if you've learned about switch statements yet, but using them would be better than having so many if statements.
- 10-03-2011, 03:04 AM #18
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
Thank you soo much!! One more thing. So I have to have commas in my print statements. In the first statement, I figured out how to correctly add the commas, but am having trouble since the last one.
System.out.printf(" You have been alive for %,d seconds.\n", secondslived);
System.out.println("The average human life is 2,500,000,000 seconds");
System.out.printf("You have" + (averagelife - secondslived) + " seconds to live!");
- 10-03-2011, 03:07 AM #19
Member
- Join Date
- Sep 2011
- Posts
- 24
- Rep Power
- 0
Re: Seconds Left to Live
Thanks so much!! One last thing. I have to add commas in the print statements. I figured out how to correctly add commas in the first print statement, but can't correctly figure it out for the last print statement.
System.out.printf("You have been alive for %,d seconds.\n", secondslived);
System.out.println("The average human life is 2,500,000,000 seconds");
System.out.printf("You have " + (averagelife - secondslived) + " seconds to live!");
- 10-03-2011, 03:11 AM #20
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Seconds Left to Live
I honestly never use printf(), since I find println() much easier.
If I were doing this then I would change
toJava Code:System.out.printf(" You have been alive for %,d seconds.\n", secondslived); System.out.println("The average human life is 2,500,000,000 seconds"); System.out.printf("You have" + (averagelife - secondslived) + " seconds to live!");
Java Code:System.out.println("You have been alive for "+secondslived+" seconds."); System.out.println("The average human life is 2,500,000,000 seconds"); System.out.println("You have "+(averagelife - secondslived)+" seconds to live!");Last edited by Solarsonic; 10-03-2011 at 03:13 AM.
Similar Threads
-
Reversing dojo slider to make it slide from right to left rather left to right as giv
By gurpreet.singh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-05-2011, 01:49 PM -
square moves left and down but not up or left
By natdizzle in forum AWT / SwingReplies: 3Last Post: 02-04-2011, 05:20 PM -
Finding epoch seconds and separate yera,month,day,hr,min and seconds
By sathish kumar in forum New To JavaReplies: 4Last Post: 09-09-2010, 11:15 AM -
how to get the seconds out of it
By baktha.thalapathy in forum New To JavaReplies: 15Last Post: 06-09-2010, 03:24 AM -
a constructor to convert seconds to hours, min & seconds
By senca in forum New To JavaReplies: 3Last Post: 04-05-2010, 01:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks