Java:手动向日期添加天数

标签 java date

我的程序读取 3 个代表日期的整数和第四个代表天数的整数,并计算天数后的日期。

我正在使用blueJ,我不明白为什么输出的日期不起作用 - 闰年不起作用,它起作用并显示为无效的唯一情况是当我输入 32/的一天时33 等等。我哪里出错了?顺便说一句,除了 if 和/或 booleans/switch 之外,我们不允许使用任何其他东西。

我直接从blueJ复制了我写的代码:

import java.util.Scanner;

public class Dates
{
    public static void main (String[]args)
    {
        int day, month, year, num;
        int daysInMonth = 0;
        final int JAN = 1;
        final int FEB = 2;
        final int MAR = 3;
        final int APR = 4;
        final int MAY = 5;
        final int JUN = 6;
        final int JUL = 7;
        final int AUG = 8;
        final int SEP = 9;
        final int OCT = 10;
        final int NOV = 11;
        final int DEC = 12;
        final int LeapYear = 29;
        final int NotLeapYear = 28;
        final int MinMonthsInYear = 1;
        final int MaxMonthsInYear = 12;

        Scanner scan = new Scanner(System.in);
        System.out.println("This program reads 3 integers representing     a date and a fourth " +
                        "integer representing amount of days, and calculates the date " +
                        "after the amount of days.");
        System.out.println("Please enter 3 integers- the day, the month and the year");                    
        day = scan.nextInt();
        month = scan.nextInt();
        year = scan.nextInt();

        switch (daysInMonth)
        {
            case JAN: 
            case MAR: 
            case MAY:
            case JUL:
            case AUG:
            case OCT:
            case DEC: 
                daysInMonth=31;
                break;
            case APR:
            case JUN:
            case SEP:
            case NOV: 
                daysInMonth=30;
                break;
            case FEB:
                if((year%400)==0 || (year%4)==0 && (year%100)!=0)
                {
                    daysInMonth=LeapYear;
                }
                else 
                {
                    daysInMonth=NotLeapYear;
                }
                break;
            default:
                System.out.println("The original date " +day + "/" +month + "/" +year + " is invalid.");
                return;
        }

        if (month<1 && month>12 || year<0)
        {
            System.out.println("The original date " +day + "/" +month + "/" +year + " is invalid.");
            return;
        }

        System.out.println("Please enter an integer which represents the number of days");    
        num = scan.nextInt();
        if (num<1 && num>10 && num<=0)
        {
            System.out.println("The number of days must be between 1-10");
            return;
        }

        System.out.println("The original date is " +day + "/" +month + "/" +year + ".");

        if (JAN>31 || MAR>31 || MAY>31 || JUL>31 || AUG>31 || OCT>31 | DEC>31)
        {
            month++;
        }   
        if (APR>30 || JUN>30 || SEP>30 || NOV>30)
        {
            month++;
        }   
        if (DEC>31)
        {
            year++;
        }

        System.out.println("After num days the date is " + day + "/" + month + "/" + year + ".");      
    }
}

最佳答案

就像 @MikeJRamsey56 所说,您的 if 语句中存在错误。还有其他一些问题,例如daysInMonth永远是0 。我不想为您单独列出它们,因为如果您自己找到它们,您会更好地理解,但需要记住以下几点:

  • ||表示“或”,如“如果 a OR b 为真”——仅当两边都为假时才会跳过该 block 。
  • &&意思是“和”,如“如果 a AND b 为真”——该 block 将被跳过除非两边都为真。
  • 仔细检查每个 block 的意图,并考虑在每个 block 上方添加注释,例如 // if the number of days is more than the current month, move on to the next month 。这可以帮助您在代码中找到实际上不是您在 if 中放入的内容的位置。声明。
  • 您可能不想说类似 JAN>31 这样的话-JAN是一个常量 ( final ),因此值始终为 1 。换句话说,该表达式相当于 1>31这永远是错误的。
  • &| (不要与 &&|| 混淆)是 bitwise-operators ;无需赘述,它们不是您通常想要使用的运算符。如果您在代码中使用它们并且您不打算进行按位运算,则结果将会有错误/损坏。
  • 创建难以理解的条件很容易 - Java 将如何解析 a || b && c || d , 例如? rules实际上已经指定了,但是仍然很容易让自己感到困惑。根据经验,最好将多个条件分组到括号中,这样您的意图就更清晰,例如a || (b && (c || d)) .
  • 逐行执行程序;传入一个您希望触发第一个条件的输入 - 是吗?如果没有,请修复它,一旦确认条件按预期工作,请继续下一步。以有序、结构化的方式进行调试可以让您划分工作并确信问题的子部分能够正常工作。然后,一旦您了解了所有子部分的工作原理,您就应该对整个事情的工作原理充满信心。这(本质上)就是unit testing背后的想法。 ,但如果您还没有探索过这个概念,请不要担心。

我希望这足以开始!

关于Java:手动向日期添加天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731158/

相关文章:

java - jfreechart,图表创建错误

java - 在 groovy 中应用一些谓词到流过滤器

java - SimpleDateFormat 和基于区域设置的格式字符串

mysql - 如何将 MySQL 中的值增加到将来的某个时间?

ruby-on-rails - 你如何在 Ruby 中实现 1.day.ago

mysql - 是否有可能在 mysql 中创建选择系列?

java - 我想在vscode最近的扩展上使用jdk8,如何在setting.json上设置

java内存大小优化

java - Apache Velocity 访问用 Java 代码创建的字符串数组?

r - 如何在 ggplot2 中添加 abline,x 轴为年份?