Hi,
I've been working in my free time on a project which tells what day someone is born based on their birthday. However, when I start to input birthdays greater than about a year old, the program is off by about a day. I believe this problem to be due to the leap year if statement I had, which I bolded in the code below. If you can offer any tips, suggestions, or hints, that would be amazing.
Code:
import java.util.Scanner; import static java.lang.System.in; import static java.lang.System.out; public class DayOfBirth { public static void main (String args[]) { Scanner scanned = new Scanner (in); out.println("Which year were you born?"); int birthYear = scanned.nextInt(); out.println("What month were you born?(1 for January, 2 for February...)"); int birthMonth = scanned.nextInt(); out.println("What day were you born?"); int birthDate = scanned.nextInt(); int daysSince = 31 - birthDate; int monthsSince = 12 - birthMonth; int yearsSince = 2018 - birthYear; int leapYearsSince = yearsSince / 4; int monthDaysSince = 0; switch (monthsSince) { case 0: monthDaysSince = 0;//From end of December break; case 1: monthDaysSince = 31;//November break; case 2: monthDaysSince = 61;//October break; case 3: monthDaysSince = 92;//September break; case 4: monthDaysSince = 122;//August break; case 5: monthDaysSince = 153;//July break; case 6: monthDaysSince = 184;//June break; case 7: monthDaysSince = 214;//May break; case 8: monthDaysSince = 245;//April break; case 9: monthDaysSince = 275;//March break; case 10: monthDaysSince = 306;//February break; case 11: monthDaysSince = 334;//January } String day = null; int yearDaysSince = yearsSince * 365; [B]if (leapYearsSince == 0 && birthYear % 4 == 0 && birthMonth <=2) { leapYearsSince++; }[/B] int daysSinceBirth = daysSince + leapYearsSince + monthDaysSince + yearDaysSince; int daysFromMonday = daysSinceBirth % 7; switch (daysFromMonday) { case 0: day = "Monday"; break; case 1: day = "Sunday"; break; case 2: day = "Saturday"; break; case 3: day = "Friday"; break; case 4: day = "Thursday"; break; case 5: day = "Wednesday"; break; case 6: day = "Tuesday"; break; } out.println("You were born on a " + day + "."); scanned.close(); } }