简体   繁体   English

使用Java中的日期验证程序获取不正确的结果

[英]Getting incorrect results with my Date Validation program in Java

I have to keep the program basic, nothing too fancy. 我必须保持程序基本,没有什么花哨的。 Here is my problem: When I enter the date "11/31/2013" it returns "Incorrect day input for a regular month with 30 days". 这是我的问题:输入日期“ 2013年11月31日”时,它返回“对于30天的常规月份,输入的日期不正确”。 I understand this means there is a problem with structure of my If/Else statement and/or my boolean expressions. 我了解这意味着If / Else语句和/或布尔表达式的结构存在问题。 Yet I can't seem to figure out the problem. 但是我似乎无法弄清楚问题所在。 Any help is appreciated, thank you. 任何帮助表示赞赏,谢谢。

Here is my program: 这是我的程序:

import java.util.Scanner;

public class DateValidation {

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    String date;
    System.out.println("Please enter a date in the following format: mm/dd/yyyy ");
    date = keyboard.next();

    int month = Integer.parseInt(date.substring(0, 2));
    int day = Integer.parseInt(date.substring(3, 5));
    int year = Integer.parseInt(date.substring(6, 10));
    boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
    boolean thirtyMonth = (month == 9 || month == 4 || month == 5 || month == 10);
    boolean regMonth = (month == 1 || month == 3 || month == 6 || month == 7 || month == 8 || month == 11 || month == 12);

    if ((month <= 0) || (day <= 0) || (day >= 32)) {
        System.out.println("You entered an incorrect month or day value");
    }
    if ((month == 2) && (isLeapYear == true) && (day > 0) && (day <= 29)) {
        System.out.println("it's a leap year - the date is " + date);
        System.exit(0);
    } else if ((month == 2) && (isLeapYear == false) && (day > 0) && (day <= 28)) {
        System.out.println("it's not a leap year - the date is " + date);
        System.exit(0);
    } else if (month == 2) {
        System.out.println("Your input is invald. Non leap-years only have 28 days");
        System.exit(0);
    }

    if ((thirtyMonth == true) && (day > 0) && (day <= 30)) {
        System.out.println("This is a valid date for a 30 day month  - here is the date " + date);
        System.exit(0);
    } else if ((thirtyMonth == true) && (day < 0) || (day >= 31)) {
        System.out.println("Incorrect day input for a regular month with 30 days");
        System.exit(0);
    }

    if ((regMonth) && (day > 0) && (day <= 31)) {
        System.out.println("This is a valid date for a regular month with 31 days - here is the date " + date);
        System.exit(0);
    } else if ((regMonth) && (day < 0) && (day >= 32)) {
        System.out.println("Incorrect day input for a regular month with 31 days");
        System.exit(0);
    }


}

} }

April,June Sep and November are 30 days month 4月,6月,9月和11月是30天一个月

boolean thirtyMonth = (month == 9 || month == 4 || month == 5 || month == 10);

should be 应该

boolean thirtyMonth = (month == 9 || month == 4 || month == 6 || month == 11);
boolean regMonth = (month == 1 || month == 3 || month == 5 ||month == 7 || month == 8 || month == 10 || month == 12);

change 更改

    } else if ((thirtyMonth == true) && (day < 0) || (day >= 31)) {

to

    } else if (thirtyMonth && (day < 0 || day >= 31)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM