0

我被困在这个问题上。

编写一个程序,输入两个日期(仅限月份和日期)并显示两个日期之间的天数。假设这两个日期都在 2013 年。程序必须验证输入的每个日期,以检查日期是否介于 1 和该月的最大天数之间。使用数组存储每月的最大天数并输出经过的天数。

由于同行/熟人复制我的代码的经验,删除了代码。

问题是每当我输入January 1第一个日期和April 1第二个日期时,它只输出31 days而真正的答案是90 days。任何人都可以帮忙吗?编辑:它现在正常工作。感谢那些帮助过的人。

4

5 回答 5

2

改变

for (int count = inputMonth; count > inputMonth2; count++)

for (int count = inputMonth; count < inputMonth2; count++)

于 2013-09-30T09:55:20.813 回答
0
public int daysBetween(Date d1, Date d2){
    return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
 }

获取java中两个日期之间的天数

于 2013-09-30T09:56:07.600 回答
0

使用java.util.Calendar

int diff = 0;

Calendar begin = Calendar.getInstance();
begin.set(Calendar.MONTH, beginMonth);
begin.set(Calendar.DAY_OF_MONTH, beginDay);

Calendar end = Calendar.getInstance();
end.set(Calendar.MONTH, endMonth);
end.set(Calendar.DAY_OF_MONTH, endDay);

while(begin.compareTo(end) <= 0) {
    diff++;
    begin.add(Calendar.DAY_OF_MONTH, 1);
}

System.out.println(diff);

这样,API 本身就可以处理月份中的天数。这也更加灵活,因为即使给定日期不在同一年,您也可以找到差异(需要对代码进行少量更改)。

于 2013-09-30T10:06:28.877 回答
0

你的逻辑有缺陷。你需要做这样的事情: -

int start = 0;
int middle = 0;
int end = 0;
int sum = 0;
...
if (code2 > code) {
    start = Days[code] - inputDay + 1;
    for (int count = inputMonth; count < code2; count++) {
        middle += Days[count];
    }
    end = inputDay2;
} else {
    start = inputDay2 - inputDay + 1;
}

sum = start + middle + end;
于 2013-09-30T10:11:10.153 回答
0

你在 for 循环中的条件使用了 > 当它应该是 <

for (int count = inputMonth; count < inputMonth2; count++) {
    middle = middle + Days[count];
}

您的代码包含大量逻辑错误(例如:使用同一个月中的 2 个日期时会发生什么情况),下面是一个工作版本:

public static void main(String[] args) {
    String monthStr;
    String dayStr;
    int inputMonth;
    int inputDay;
    String monthStr2;
    String dayStr2;
    int inputMonth2;
    int inputDay2;
    int code;
    int code2;
    int start;
    int middle = 0;
    int end;
    int sum;

    String[] Months = { "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November",
            "December" };

    int[] Days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    monthStr = JOptionPane.showInputDialog(null, "Enter First Month: ");
    inputMonth = Integer.parseInt(monthStr);

    dayStr = JOptionPane.showInputDialog(null, "Enter First Day: ");
    inputDay = Integer.parseInt(dayStr);

    monthStr2 = JOptionPane.showInputDialog(null, "Enter Second Month: ");
    inputMonth2 = Integer.parseInt(monthStr2);

    dayStr2 = JOptionPane.showInputDialog(null, "Enter Second Day: ");
    inputDay2 = Integer.parseInt(dayStr2);

    if (inputMonth == inputMonth2) {
        // Same months
        sum = inputDay2 - inputDay;
    } else {
        // Different months
        start = Days[inputMonth - 1] - inputDay;
        end = inputDay2;
        middle = 0;

        // Start at inputMonth+1, start already included the days remaining
        // in inputMonth
        for (int count = inputMonth + 1; count < inputMonth2; count++) {
            middle = middle + Days[count - 1];
        }
        sum = start + middle + end;
    }
    JOptionPane.showMessageDialog(null, "DAYS COMPUTATION PROGRAM"
            + "\nFirst Date: " + Months[inputMonth - 1] + " " + inputDay
            + "\nSecond Date: " + Months[inputMonth2 - 1] + " " + inputDay2
            + "\nNumber of Days Elapsed: " + sum + " Days");
}
于 2013-09-30T10:12:11.993 回答