我的程序讀取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