【问题标题】:How do I print the number of days of all 12 months using this method如何使用此方法打印所有 12 个月的天数
【发布时间】:2019-02-21 02:22:49
【问题描述】:

我的代码将用户输入作为年、月和日期。我有三种方法,一种获取星期几,一种获取该月的天数,另一种计算该年是否为闰年。

当用户输入年月和日期时,例如“2016 3 3”,我希望我的代码列出从 1 到 12 的月份,并在每个数字旁边列出该月的天数。我对所有三种方法的代码如下。

class Date {

int year, month, day;

Date(int y, int m, int d) {
    year = y;
    month = m;
    day = d;
}

/**
 * This method returns the day of the week as an integer: 0 and 6: Sunday
 * and Saturday 1 - 5: Weekdays
 */
public int getDayOfWeek() {

    int y0 = year - (14 - month) / 12;
    int x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
    int m0 = month + 12 * ((14 - month) / 12) - 2;
    int d0 = (day + x + (31 * m0) / 12) % 7;

    return d0;
}
/**
 * This method returns the number of days in a given month an integer
 */
public int getDaysInMonth(int month) {

    int daysInMonth = (int) (28 + (Math.floor(month / 8.0) + month) % 2 + 2 % month + 2 * Math.floor(1.0 / month));

    if (month == 2 && isLeapYear()) {
        daysInMonth += 1;
    }
    return daysInMonth;
}


public boolean isLeapYear() {

    boolean isLeapYear = true;

    if (year % 4 != 0) {
        isLeapYear = false;
    }
    else {  
        if (year % 100 != 0) {
            isLeapYear = true;
        }
        else if (year % 400 != 0) {
            isLeapYear = false;
        }
    else { 
            isLeapYear = true;
         }
    }



    return isLeapYear;
} 

我在计算机科学专业的第一年,对此仍然很陌生,我一直盯着这段代码并在谷歌上搜索了一天的大部分时间,但似乎无法弄清楚任何事情,任何帮助将不胜感激。

我知道这是错误的,但这就是我目前所能想到的全部

public void printDaysInMonth() {
    int m = getDaysInMonth(month);
    System.out.println("Month  " + "  Days");
    for (int i=0; i<12; i++) {
        System.out.println(m);
    }
}

【问题讨论】:

  • printDaysInMonth()for循环中,考虑打印getDaysInMonth(i)

标签: java methods int


【解决方案1】:

您在正确的轨道上,但是您在 for 循环之外分配了您的 m 变量,因此每次都打印相同的月份。相反,尝试分配它并在你已经拥有的循环中打印它:

public void printDaysInMonth() {
    for (int i = 1; i <= 12; i++) {
        int m = getDaysInMonth(i);
        System.out.println("Month " + i + " has " + m  + "days.");
    }
}

由于您的getDaysInMonth 方法已经考虑了闰年,这应该足够了!

【讨论】:

  • 谢谢!这对我帮助很大!解释也超级清楚,我也很容易理解
【解决方案2】:

在 Java 8 中,您可以使用以下代码进行简单的日期操作:

public static void main(String[] args) {
    LocalDate date1=getDate(12,12,2020);

int dayOfWeek=getDayOfWeek(date1);

boolean isLeapYear=isLeapYear(date1);
    System.out.println("date: "+date1);
    System.out.println("dayOfWeek: "+dayOfWeek);
    System.out.println("is Leap year: "+isLeapYear);
}

// 将返回 LocalDate 值

private static LocalDate getDate(int date,int month, int year){
    return LocalDate.of(year,month,date);
}

// 将返回 Dayofweek

private static int getDayOfWeek(LocalDate date){
    return date.getDayOfWeek().getValue();
}

// 如果年份是/不是闰年,将返回真/假

private static boolean isLeapYear(LocalDate date){
    return date.isLeapYear();
}

/* 如果要打印特定年份的所有月份的日期,请使用以下方法*/

public void printLengthsOdDaysInMonthsOfYearPassed(int year){
    year=2020;
    for(int i=1;i<=12;i++){
        LocalDate date2=LocalDate.of(year,i,1);

        int length=date2.getMonth().maxLength();
        System.out.println("Month: "+i+ " length: "+length);
        System.out.println("is leap year: "+date2.isLeapYear());
    }
}

【讨论】:

  • 我认为 OP 试图手写计算作为练习,因此可能不会接受使用库方法的解决方案。对于生产代码,库方法是唯一正确的方法,因此其他人都可以从这个答案中受益。
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 2018-09-07
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 2013-09-10
相关资源
最近更新 更多