玩具程序=日历,它计算您的生日直到生日 - 循环无法正常工作

时间:2015-07-08 17:56:59

标签: java loops while-loop

我刚刚进入java并为自己构建一个简单的玩具程序,该程序会告诉您生日何时基于当前日期并且还会显示简单的日历。然而。我似乎陷入了"案例1"循环,无法正常工作。同一个案例2工作正常,案例1由于某种原因仅适用于8月和之后的几个月,因为没有正确计算。另一年的案例3尚未实施。

有人请求帮助,已经使用了4个小时,并且无法弄清楚它为什么不起作用。

public static int today;
public static int ThisMonth;
public static int MonthSwitch;
public static int DaysUntilBD;


public static void main (String args[]){
    GregorianCalendar d = new GregorianCalendar(); 

    ThisMonth = d.get(Calendar.MONTH);
    ThisMonth++;
    today = d.get(Calendar.DAY_OF_MONTH);


    String MonthofBirth = JOptionPane.showInputDialog("Month of birth");
    int month = Integer.parseInt(MonthofBirth);
    String DayOfBirth = JOptionPane.showInputDialog("Day of Birth");
    int birth = Integer.parseInt(DayOfBirth);

    d.set(Calendar.MONTH, month);
    int CurrentDay = d.get(Calendar.DAY_OF_MONTH);
    int CurrentMonth = d.get(Calendar.MONTH);

    //switches
    if(month > ThisMonth) MonthSwitch=1; //same year, month in future
    if(month == ThisMonth) MonthSwitch=2; //same month
    if(month < ThisMonth) MonthSwitch = 3; //birthday next year, not implemented yet


    switch(MonthSwitch) {
    case 1:
        month++;
        while(month!=ThisMonth)
        {
            System.out.println(ThisMonth);
            DaysUntilBD++;        
            d.add(Calendar.DAY_OF_MONTH, 1);
            ThisMonth=d.get(Calendar.MONTH);

            int CheckDay = d.get(Calendar.DAY_OF_MONTH);
            int CheckMonth= d.get(Calendar.WEEK_OF_MONTH);
            if (CheckDay <=7 && CheckMonth !=1) d.add(Calendar.MONTH, 1);
        }
        today=d.get(Calendar.DAY_OF_MONTH);
        while (birth<today){
            DaysUntilBD++;        
            d.add(Calendar.DAY_OF_MONTH, 1);
            today=d.get(Calendar.DAY_OF_MONTH);
        }
        break;    

    case 2:
        while(birth!=today)
        {
            DaysUntilBD++;        
            d.add(Calendar.DAY_OF_MONTH, 1);
            today=d.get(Calendar.DAY_OF_MONTH);
        }
        break;

    }

    month=month-1;  
    d.set(Calendar.MONTH, month);
    d.set(Calendar.DAY_OF_MONTH,1);
    int DayOfTheWeek = d.get(Calendar.DAY_OF_WEEK);


    for (int i = Calendar.SUNDAY; i < DayOfTheWeek; i++ )
        System.out.print("  ");

    do {
        int day = d.get(Calendar.DAY_OF_MONTH);
        if(day<10) System.out.print(" ");
        System.out.print(day);

        if (day == birth) System.out.print("*");

        else System.out.print(" ");

        if (DayOfTheWeek == Calendar.SUNDAY)
            System.out.println();

        d.add(Calendar.DAY_OF_MONTH, 1);
        DayOfTheWeek = d.get(Calendar.DAY_OF_WEEK);
    } while (d.get(Calendar.MONTH) == month);

    System.out.println();

    System.out.print("Your birthdays are in "+ DaysUntilBD);
    System.out.print(" days");

}

2 个答案:

答案 0 :(得分:0)

您应该使用Calendar.DAY_OF_YEAR。这将使它更容易

下面的伪代码几乎就是你想要的;你还需要考虑闰年。

Calendar today = ...;
Calendar futureDate = ...;

int days = (today.get(YEAR) - futureDate(YEAR)) * 365;
days = days + futureDate.get(DAY_OF_YEAR) - today.get(DAY_OF_YEAR);

答案 1 :(得分:0)

问题是,一旦您进入case 1,就会启动while循环。离开循环的条件是month!=ThisMonth,但它们都没有在循环内部发生变化,它们始终保持一致,因此循环是无限的。
您应该在循环内更改month,而不是在month++之前更改while。 正如@ControlAltDel所说 - 比较日期有更好的选择,但这对于学习目的来说是一个很好的练习。