0

如果生日在当前月份之后的月份,计算器只会返回正确的值。我已经为此苦苦挣扎了一段时间,但我不知道如何解决这个逻辑错误。请,任何帮助将不胜感激。

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.");
        }
    }
}
4

1 回答 1

0
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;
        } else {
            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;
        } else {
            totalDays = totalDays + 30;
        }
    }
    totalDays = 365 - totalDays;
}

你忘了把 else 块totalDays = totalDays + 30;

于 2013-06-19T04:06:44.803 回答