我的程序读取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 + ".");
}
}
数字如何小于1且大于10? if(num <1 && num> 10 && num <= 0)。你不是说如果(号码<1 || num > 0)?两个垂直条是逻辑或操作符。两个&符号是逻辑运算符。 – MikeJRamsey56
“开关(daysInMonth)”是错误的。 daysInMonth应该是28到31或0的东西,如果初始化,但你的案例询问从1到12的数字。编辑:看过你的其他条件,如“if(DEC> 31)”,我得出结论,你在进一步做任何事之前,应该回到基础知识。你似乎没有完全理解条件是如何工作的。您可能想要创建一个测试用例,首先在纸上对其进行评估,然后使用调试器对其进行测试,以确定程序是否完全相同。因为在非常多的位置,它并没有达到预期的目的。 – Aziuth