Java - Days Computation Program

时间:2013-09-30 09:53:46

标签: java

我坚持这个问题。

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

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

问题是,每当我输入January 1作为第一个日期,April 1作为第二个日期,它只输出 31天,而实际答案 90天即可。有人可以帮忙吗? 编辑: 它现在正常工作。感谢那些帮助过的人。

5 个答案:

答案 0 :(得分:2)

更改

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

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

答案 1 :(得分:0)

public int daysBetween(Date d1, Date d2){
    return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
 }

来自Getting the number of days between two dates in java

答案 2 :(得分: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本身就可以处理几个月的天数。此外,这也更加灵活,因为即使给定日期不在同一年,也可以找到差异(代码中需要进行少量更改)。

答案 3 :(得分: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;

答案 4 :(得分:0)

你在for循环中的条件使用了&gt;什么时候应该&lt;

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