0
如果生日在當月之後的月份,計算器只返回正確的值。我一直在努力這一段時間,我不知道如何解決這個邏輯錯誤。請,任何幫助將非常感激。Java生日計算邏輯錯誤
import java.util.*;
public class BirthdayCalculator {
public static void main(String[] args) {
System.out.println("Enter the current month numerically (i.e. if it is July, enter 7)");
Scanner console = new Scanner (System.in);
int monthIs = console.nextInt();
System.out.println("Enter today's date");
int todayIs = console.nextInt();
System.out.println("Enter person 1's birth month");
int birthMonthOne = console.nextInt();
System.out.println("Enter person 1's birthday");
int birthDayOne = console.nextInt();
System.out.println("Enter person 2's birth month");
int birthMonthTwo = console.nextInt();
System.out.println("Enter person 2's birthday");
int birthDayTwo = console.nextInt();
int daysUntilEndOfMonth = getMonth(monthIs, todayIs, 0);
int totalDaysOne = monthsUntilBirthMonth(monthIs, birthMonthOne, 0);
int totalDaysTwo = monthsUntilBirthMonth(monthIs, birthMonthTwo, 0);
finalReport(daysUntilEndOfMonth, totalDaysOne, totalDaysTwo, birthDayOne, birthDayTwo);
}
public static int getMonth(int monthIs, int todayIs, int daysUntilEndOfMonth) {
if (monthIs == 2) {
daysUntilEndOfMonth = 28 - todayIs; //days until end of february
} else if (monthIs == 1 || monthIs == 3 || monthIs == 5 || monthIs == 7 || monthIs == 8 || monthIs == 10 || monthIs == 12) {
daysUntilEndOfMonth = 31 - todayIs; //days until end of months with 31 days
} else {
daysUntilEndOfMonth = 30 - todayIs; //days until end of month with 30 days
}
return daysUntilEndOfMonth;
}
public static int monthsUntilBirthMonth(int initialMonth, int endMonth, int totalDays) {
if (initialMonth == 12) { // makes sure that largest month is december
initialMonth = 1;
} else {
initialMonth = initialMonth + 1;
}
if (initialMonth < endMonth) {
for (int i = initialMonth; i < endMonth; i++) { //adds total days until birthday until month of birthday is reached
if (i == 2) {
totalDays = totalDays + 28;
} else if (i == 1 || i == 3 || i == 5 ||i == 7 || i == 8 || i == 10 || i == 12) {
totalDays = totalDays + 31;
}
totalDays = totalDays + 30;
}
} else if (initialMonth > endMonth) { //if the birthday is earlier than the current month,
for (int i = endMonth; i <= initialMonth - 1; i++) { //adds total days until birthday until month of birthday is reached
if (i == 2) {
totalDays = totalDays + 28;
} else if (i == 1 || i == 3 || i == 5 ||i == 7 || i == 8 || i == 10 || i == 12) {
totalDays = totalDays + 31;
}
totalDays = totalDays + 30;
}
totalDays = 365 - totalDays;
}
return totalDays;
}
public static void finalReport(int daysUntilEndOfMonth, int totalDaysOne, int totalDaysTwo, int birthDayOne, int birthDayTwo) {
int untilOne = daysUntilEndOfMonth + totalDaysOne + birthDayOne; //calculates days until first person's birthday
int untilTwo = daysUntilEndOfMonth + totalDaysTwo + birthDayTwo; //calculates days until second person's birthday
System.out.print("There are " + untilOne + " days until person 1's birthday\n"); //reports number of days until birthday 1
System.out.print("There are " + untilTwo + " days until person 2's birthday\n"); //reports number of days until birthday 1
if (untilOne < untilTwo) { //figures out which birthday is sooner
System.out.println("Person 1's birthday is sooner.");
} else {
System.out.println("Person 2's birthday is sooner.");
}
}
}
請在這裏發表您的代碼在這個網站你的問題。讓我們儘可能地幫助你。 –
@HovercraftFullOfEels請小心你所要求的;) – MadProgrammer
我有一個詞給你[JodaTime](http://joda-time.sourceforge.net/)。將您的整個代碼縮減爲4行 – MadProgrammer