【问题标题】:Multiple If Statements for leap year logic, how to structure them?闰年逻辑的多个If语句,如何构造它们?
【发布时间】:2014-10-05 13:14:57
【问题描述】:

所以在这段代码中,我试图让用户输入一年和一个月(前 3 个字母),然后确定该特定月份包含多少天。由于涉及闰年的困难,我在编码用户输入“Feb”作为月份的部分时遇到了麻烦。当我测试它时,它说: “2002 年 2 月有 29 天” "2002 年 2 月有 28 天"

如何让它只显示 29 天?

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a year: ");
    int year= input.nextInt();
    input.nextLine();
    System.out.println("Enter a month (first 3 letters with the first letter in uppercase): ");
    String month = input.nextLine();

    // leap year logic starts here
    if (year % 4 == 0 || "Feb".equals(month)) {
        System.out.println( month + year + "has 29 days");
    }
    else if (year % 4 > 0 || "Feb".equals(month)) {
        System.out.println( month + year + "has 28 days");
    }
    else if("Jan".equals(month) || "Mar".equals(month) ||
            "May".equals(month) || "July".equals(month) ||
            "Aug".equals(month) || "Oct".equals(month) ||
            "Dec".equals(month)) {
        System.out.println(month + year + "has 31 days");
    }
    else if ("Apr".equals(month)|| "Jun".equals(month) ||
             "Sep".equals(month) || "Nov".equals(month)) {
        System.out.println(month + year + "has 30 days");
    }

【问题讨论】:

  • 2002 年 2 月不是只有 28 天吗?
  • 没错,我需要让它不这么说。
  • 危险:每100年不是闰年,但每400年是一个。
  • 对于所有没有投票权的人,我想让你知道......这实际上是几乎每个新的 Java 编码器的一个非常重要的问题。

标签: java if-statement


【解决方案1】:

这只是您的条件语句中的一个简单的逻辑缺陷。你的 OR 应该是 AND

if (year % 4 == 0 && "Feb".equals(month)){
    // Note the && above
    System.out.println( month + year + "has 29 days");
}
else if (year % 4 > 0 && "Feb".equals(month)){
    // Note the && above
    System.out.println( month + year + "has 28 days");
}

每个条件句都是 (A || B)。第一个条件 year%4==0 被评估为真,因此甚至没有评估第二个条件(这称为布尔短路)。

【讨论】:

    【解决方案2】:

    除了使用“and” (&&) 之外,您还需要为构成闰年的各种条件嵌套它们:

    boolean feb = "Feb".equals(month);
    if (feb) {
        if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
        { System.out.println("has 29 days"); }
        else { System.out.println("has 28 days"); }
    } else if (....) { }
    

    不过,我会使用带开关的数字月份:

    // NB: if you use Date.getMonth() add +1
    switch(month) {
      case 1,3,5,7,9,10,12:
        System.out.println("31 days");
        break;
      case 4,6,7,11:
        System.out.println("30 days");
        break;
      case 2:
        if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
          System.out.println("has 29 days");
        } else {
          System.out.println("has 28 days");
        }
        break;
      }
    

    当然,最好的闰年功能是您不自己编程的功能。最好查看系统库:http://www.java2s.com/Tutorial/Java/0040__Data-Type/Getthelastdayofamonth.htm

    【讨论】:

    • 我也会使用month = input.nextLine().toLowerCase(Locale.ENGLISH);,然后您可以使用“jan”、“feb”、“mar”作为要比较的字符串,因为无论用户是否使用大写字母都无关紧要。你可以使用最终的else { System.out.println("Unknown Month " + month); }
    • 感谢您的建议!不幸的是,我正在做的作业要求它是大写字母。
    猜你喜欢
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多