Reputation: 11
My program reads 3 integers representing a date and a fourth integer representing a number of days, and calculates the date after the number of days.
I am using blueJ, and I don't understand why the outputted dates don't work - the leap years don't work, the only cases it works and comes out as invalid is when I enter a day of 32/33 etc. Where am I going wrong? By the way, we are not allowed to use any other things except if's and/or booleans/switch.
I copied the code I wrote straight from 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 + ".");
}
}
Upvotes: 1
Views: 122
Reputation: 48874
Like @MikeJRamsey56 said, there are bugs in your if statements. There are also a few other issues, such as daysInMonth
always being 0
. I don't want to call them all out for you individually as you'll understand it better if you find them yourself, but here's some points to remember:
||
means "or" as in "if a OR b is true" - the block is skipped only if both sides are false.&&
means "and" as in "if a AND b is true" - the block is skipped unless both sides are true.// if the number of days is more than the current month, move on to the next month
. This can help you spot place in your code where that's not actually what you've put in the if
statement.JAN>31
- JAN
is a constant (final
), and therefore always has a value of 1
. That expression, in other words, is equivalent to 1>31
which will always be false.&
and |
(not to be confused with &&
and ||
) are bitwise-operators; without going into too much detail they're not operators you will normally want to use. If you use them in your code and you didn't intend to do bitwise operations the results will be buggy/broken.a || b && c || d
, for instance? The rules are actually specified, but it's still easy to confuse yourself. As a rule-of-thumb it's a good idea to group multiple conditionals into parentheses so your intent is clearer, e.g. a || (b && (c || d))
.I hope that's enough to get started!
Upvotes: 1