【问题标题】:Why doesn't my date validation code work?为什么我的日期验证码不起作用?
【发布时间】:2017-02-26 04:08:59
【问题描述】:

我的代码没有返回任何值,我不知道为什么。我的任务要求我编写一个接受 mm/dd/yyyy 格式的日期的代码,并且我需要输入闰年。问题是,我没有得到任何输入。我是一个业余广告,我不知道出了什么问题。我也允许使用 Case 语句,但我不确定如何实现 case。

import java.util.Scanner;
public class Question1 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in).useDelimiter("/");
        System.out.println("Please enter a date in mm/dd/yyyy format: ");
        String mm = sc.next();
        String dd = sc.next();
        String yyyy = sc.next();
        int month = Integer.parseInt(mm);
        int day = Integer.parseInt(dd);
        int year = Integer.parseInt(yyyy);

        if (month <= 0 || month>12)
        {
            System.out.println("invalid month ");
        }
        if (year%4 != 0 || month == 02 || day >= 29)
        {
           System.out.println("invalid date");
        } 
        if (month == 4 || month == 6 || month == 9 || month == 11 || day >= 31)
        {
           System.out.println("Invalid day");
        }
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 || day >=32 )
        {
           System.out.println("Invalid day");
        }
        else
        {
           System.out.println("Valid date");
        }  
    }
}

【问题讨论】:

  • 那么,year 不能被 4 整除总是“无效日期”?真的吗?确定你不是说&amp;&amp; 而不是||?在许多这些检查中?你知道其中的区别,对吧?
  • 修复它,仍然没有输出。现在怎么了?
  • @BamdadGoudarziMoazami 您的代码不会返回任何值,因为该方法是无效的。你想返回什么?也许你可以发布整个作业要求我很难理解你的目标是什么
  • @kalenpw - 在上下文中,他显然是指输出 - 请参阅所有 System.out.println() 语句。 @Andreas 是的,他的逻辑是错误的 - 这可能是因为当代码永远不会超过第二行时,它变得难以调试。

标签: java date


【解决方案1】:

代码将分隔符设置为/。然后输入类似12/25/2016 的内容。第一个sc.next() 调用得到12。第二个得到25。第三个......等待,因为它没有看到另一个/,所以它不知道你已经完成了。如果您使用当前代码键入 12/25/2016/,它至少会给出输出,即使该输出还不正确。

【讨论】:

    【解决方案2】:

    你想使用 switch case 然后通过下面的代码:

    import java.util.Scanner;
    public class Question1 {
    
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in).useDelimiter("/");
    
            System.out.println("Please enter a date in mm/dd/yyyy/ format: ");
            String mm = sc.next();
            String dd = sc.next();
            String yyyy = sc.next();
            int month = Integer.parseInt(mm);
            int day = Integer.parseInt(dd);
            int year = Integer.parseInt(yyyy);
            boolean valid = isValidDate(day,month,year);
            if (valid == true)
            {
                System.out.println("Date is Valid");
            }
            else
            {
                System.out.println("Date is InValid");
            }
        }
        public static boolean isValidDate(int day, int month ,int year)
        {   
            boolean monthOk = (month >= 1) && (month <= 12);
            boolean dayOk = (day >= 1) && (day <= daysInMonth(year, month));
    
            return (monthOk && dayOk);
        }
        private static int daysInMonth(int year, int month) {
            int daysInMonth;
    
        switch (month) {
        case 1: 
        case 3: 
        case 5: 
        case 7: 
        case 8: 
        case 10: // go through
        case 12:
            daysInMonth = 31;
            break;
        case 2:
            if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                daysInMonth = 29;
            } else {
                daysInMonth = 28;
            }
            break;
        default:
            // returns 30 even for nonexistant months 
            daysInMonth = 30;
        }
        return daysInMonth;
      }    
    
    }
    

    输入 12/25/2016/,而不是 12/25/2016

    【讨论】:

    • 这位朋友。这很有帮助。
    【解决方案3】:

    这里有一些东西可以帮助你开始:

        final String DELIMITER = "/";
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a date in mm/dd/yyyy format:\n ");
    
        String date = sc.next();
        sc.close();
    
        String[] dateParts = date.split(DELIMITER);
        //check : if dateParts size is not 3 ....something is wrong
    
        String mm = dateParts[0];
        String dd = dateParts[1];
        String yyyy = dateParts[2];
    
        System.out.println(mm+" "+ dd +" "+ yyyy);
    

    【讨论】:

      【解决方案4】:

      看来你把 else 放在了错误的地方。假设您的第二个条件是正确的,而所有其他条件都是错误的,那么您的程序也会将其显示为有效日期,并且在相反的一侧也是如此。 例如,假设任何日期的第 30 天,那么它将满足第二个条件,它会显示“无效日期”。

      You should write if else as follows.
      If{
           If{
                If{
                  }
             }
      }else{
      }  
      

      所有 if 必须在嵌套的 if 和 then else 中。你的 if else 顺序是错误的。

      【讨论】:

      • 除了不正确的大写和不正确的缩进之外,这是错误的。它会转移问题而不是解决问题。如果第三个if 失败会怎样?它不会命中else 块。
      • 您只需要检查执行第 5 个条件,仅当上述 4 个条件失败时。就是这样
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 2021-05-20
      • 2017-07-03
      相关资源
      最近更新 更多