2016-11-21 76 views
1

我的程序读取3个代表日期的整数和代表天数的第四个整数,并计算日期后的日期。Java:将日期手动添加到日期

我正在使用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 + ".");  
    } 
} 
+0

数字如何小于1且大于10? if(num <1 && num> 10 && num <= 0)。你不是说如果(号码<1 || num > 0)?两个垂直条是逻辑或操作符。两个&符号是逻辑运算符。 – MikeJRamsey56

+0

“开关(daysInMonth)”是错误的。 daysInMonth应该是28到31或0的东西,如果初始化,但你的案例询问从1到12的数字。编辑:看过你的其他条件,如“if(DEC> 31)”,我得出结论,你在进一步做任何事之前,应该回到基础知识。你似乎没有完全理解条件是如何工作的。您可能想要创建一个测试用例,首先在纸上对其进行评估,然后使用调试器对其进行测试,以确定程序是否完全相同。因为在非常多的位置,它并没有达到预期的目的。 – Aziuth

回答

1

像@ MikeJRamsey56说,有虫子在你的if语句。还有一些其他问题,例如daysInMonth总是0。我不希望他们都单独叫出了你,你可以更好地了解它,如果你自己找到他们,但这里的一些要点要记住:

  • ||手段“或”中“如果OR b是真实的“ - 仅当双方都是假的,块才被跳过
  • &&的意思是“和”,如“如果AND b是真实的” - 该块被跳过,除非双方均为真。
  • 仔细检查每个块的意图,并考虑在每个块之上添加注释,如// 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,它总是错误的。
  • & and |(不要与&&||混淆)是bitwise-operators;没有太多细节,他们不是你通常想要使用的操作员。如果你在你的代码中使用它们,并且你不打算按位操作,结果将会出错/中断。
  • 很容易创建难以理解的条件 - 例如Java如何解析a || b && c || drules实际上是指定的,但它仍然很容易迷惑自己。作为一个经验法则,将多个条件组合成括号是一个好主意,所以你的意图更清晰。 a || (b && (c || d))
  • 逐行扫描您的程序;传递一个你期望触发你的第一个条件的输入 - 做到了吗?如果没有,那么一旦确认有条件的工作正如预期的那样进行下一步,就修复它。以有序,结构化的方式进行调试,可以让您划分工作范围,并确信问题的子部分正常工作。然后,一旦你知道所有的子部分工作,你应该确信整个事情的工作。这(实质上)unit testing背后的想法,但不要担心,如果这是一个你还没有探索的概念。

我希望这足以开始!

+0

谢谢dimo,我会把这个信息再试一次,但我有一个问题,如果|和&在这里不是很适合,所以我能做什么? – Tofixx

+0

@Tofixx在你的评论中你的问题没有意义。在答案dimo414解释了单字符|和&是按位运算符而不是你想要的。双字符||和&&具有完全不同的含义。请重新阅读此答案,如果不解决,请重新提出您的问题。 –

+0

@Tofixx澄清了这一点子弹点。 – dimo414