我一直在一個程序,告知用戶,如果日期是在這個格式爲「MM/DD/YYYY」日期驗證問題的Java程序
有效的,但我的程序不管什麼說,每日期是否有效。 有人可以幫助我,並告訴我我的問題在哪裏,如果是這樣解釋什麼是錯的。必須涉及閏年。並且必須給出我認爲我可以弄清楚的錯誤信息。
這是我的代碼,請幫助我。我是初學者所以謝謝你的批評
/**
* Created by MacOSX on 9/18/2016.
*/
import java.util.Scanner;
public class ASS4ID1773 {
public static void main(String[] args) {
}
{
System.out.println("Enter any date in ''mm/dd/yyyy'' format. ");
String date = "";
Scanner keyboard = new Scanner(System.in);
int mm = 00;
int dd = 00;
int yyyy = 0000;
date = keyboard.nextLine();
boolean LeapYear;
mm = 0;
dd = 0;
yyyy = 0;
LeapYear = false;
if (yyyy % 4 == 0 && (!(yyyy % 100 == 0) || yyyy % 400 == 0)) {
LeapYear = true;
}
/**
* month restrictions.
*/
if ((mm < 12) & (mm > 1)) {
System.out.println("You have entered an invalid month. Please try entering a month that exists.");
}
/**
* Day Restriction
*/
if ((dd > 31 && dd < 1)) {
System.out.println("You have entered an invalid day. Please try entering a day that exists.");
}
/**
* Months with 31 days
*/
if ((mm == 9 & mm == 4 & mm == 6 & mm == 11) & !(dd == 31)) {
System.out.println("For the month you have entered, you have entered an incorrect day.");
}
/**
* February month
*/
if ((mm == 2 && !(dd < 29)) && LeapYear == false) {
System.out.println("You have entered a day that does not exist in the month of February.");
}
/**
* Leap Year for February but with incorrect day.
*/
if ((mm == 2 & (dd < 30)) & LeapYear == true) {
System.out.println("You have entered an invalid day for the month of February.");
} else {
System.out.println("You have entered a valid date in the correct format.");
}
/**
* Leap Year for February but with correct day.
*/
if (LeapYear) {
if ((mm == 2 & (dd == 29)) & LeapYear == true) {
System.out.println(date + " is a valid date.");
}
} else {
System.out.println(date + "is not valid month must have 29 days or less.");
}
if ((mm == 2) && (dd <= 28)) {
System.out.println(date + " is a valid date.");
}
}
}
你讀日期爲一個字符串,但沒有用它做什麼... –
好了,你有一個叫做'date'字符串。你在做什麼把這些值放在'mm','dd'或'yyyy'中? –
你的'mm','dd'和'yyyy'總是*爲零;因爲這就是你設定的(兩次)。 –