验证日期从用户输入

问题描述:

我正在写一个程序,我应该有用户输入一个从0 - 4000年的日期。我应该看看日期是否有效,如果它是一个飞跃一年或没有。我在代码中遇到问题。 我得到了一个没有错误的第57行。 我也不确定如何说如果日期有效或不是如何说。 IE:这个日期是有效的,是一个闰年 - 或无效不是一个闰年...等等...验证日期从用户输入

我还是一个初学者,所以我不想为我写的代码,但我想知道如何解决它!谢谢。

import java.util.*; 

public class LegalDate //file name 
{ 
     public static void main (String [] args) 

    { 
     Scanner kb = new Scanner (System.in); //new scanner 
     //name the variables 
     int month, day, year; 
     int daysinMonth; 
     boolean month1, year1, day1; 
     boolean validDate; 
     boolean leapYear; 



     //ask the user for input 
     //I asked the MM/DD/YYYY in seperate lines to help me visually with the program 
     System.out.println("Please enter the month, day, and year in interger form: "); 
     kb.nextInt(); 

     //now I'm checking to see if the month and years are valid 
     if (month <1 || month >12) 
      { month1 = true;} 
     if (year <0 || year >4000) 
      {year1= true;} 

     //I'm using a switch here instead of an if-else statement, which can also be used 


      switch (month) { 
       case 1: 
       case 3: 
       case 5:    //months with 31 days 
       case 7: 
       case 8: 
       case 10: 
       case 12: 
        numDays = 31; 
        break; 
       case 4: 
       case 6:    //months with 30 days 
       case 9: 
       case 11: 
        numDays = 30; 
        break; 

       case 2: 
        if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) //formula for leapyear 
         numDays = 29; 
          { 
           system.out.println("is a leap year"); 
           } 
        else 
         numDays = 28; 
          { 
           system.out.println("is not a leap year"); 
           } 
        break; 
       default: 
        System.out.println("Invalid month."); 
       break; 

        if (month1 == true) 
        if (day1 == true) 
        if (year1 == true) 
          System.out.println ("date is valid "); 

        else 
        if (month1 == false) 
          System.out.println ("date is invalid"); 

        else 
        if (day1 == false) 
          System.out.println ("date is invalid"); 

         else 
         if (year1 == false) 
          System.out.println ("date is invalid"); 



    }} 

} 

您似乎没有在大括号内的'if'和'else'语句之间放置代码,这意味着该语句只适用于下一行。例如:

if (a) 
    b = true 
    c = true 
else 
    d = true 

读作

if (a) { 
    b = true 
} 
c = true 
else { 
    d = true 
} 

希望,你可以看到,编译器会不明白这一点,作为一个“其他”语句必须“如果”块及其相关的后直接发生。

我建议添加一些方法来简化您的代码。例如:

public static boolean isLeapYear(int year) { 
    return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)); 
} 

此外,如果您使用布尔变量来存储信息,您可以在最后整齐地打印它。例如,您可以在代码顶部将变量“isValid”实例化为true,如果计算日期无效,则将其设置为false,并在最后使用if语句打印结果。

我知道你说过你不想让它为你写,但这是证明方法重要性的最简单方法。希望你能看到它比你的版本更具可读性吗?

import java.util.Scanner; 

public class LegalDate { 

    static final int maxYear = 4000; 

    public static void main (String [] args) { 
     int month, day, year; 
     boolean leapYear, validDate = false; 

     Scanner kb = new Scanner (System.in); 
     System.out.println("Please enter the month, day, and year in interger form."); 

     System.out.print("Month: "); 
     month = kb.nextInt(); 
     System.out.print("Day: "); 
     day = kb.nextInt(); 
     System.out.print("Year: "); 
     year = kb.nextInt(); 

     leapYear = isLeapYear(year); 
     validDate = isValidDate(month, day, year); 

     System.out.printf("%nThe date is %svalid and is %sa leap year.%n", validDate ? "" : "not ", leapYear ? "" : "not "); 
     kb.close(); 
    } 

    public static int numDaysInMonth(int month, boolean isLeapYear) { 
     switch (month) { 
     case 1: 
     case 3: 
     case 5: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      return 31; 
     case 4: 
     case 6: 
     case 9: 
     case 11: 
      return 30; 
     case 2: 
      if (isLeapYear) { 
       return 29; 
      } else { 
       return 28; 
      } 
     default: 
      return 0; 
     } 
    } 

    public static boolean isLeapYear(int year) { 
     return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)); 
    } 

    public static boolean isValidDate(int month, int day, int year) { 
     return (month >= 1 && month <= 12) && (day >= 1 && day <= numDaysInMonth(month, isLeapYear(year))) && (year >= 0 && year <= maxYear); 
    } 
} 

如果您有任何问题,我会尽我所能来回答他们!

+0

谢谢!我知道我的代码是草率的,但我不知道它是多么的分散。感谢您的帮助! – cargoboom

+0

我acuually有一个问题,我们还没有得到这部分尚未在我的课程'System.out.printf(“%n日期是%s有效,是%闰年%n”,validDate?“”:“不是“,leapYear?”“:”不“)kb.close(); “我一直在试图找出它是什么,但你怎么用这个?你用它来取代if-else语句吗? – cargoboom

+1

printf是一种用变量替换占位符(%s代表字符串,%d代表数字等)的方法,%n是换行符。 'condition'? 'execute_if_true':'execute_if_false'就像你所说的那样,是if/else的简写形式。 因此,如果日期无效,则在第一个%s中插入“not”,如果日期不是闰年,则类似地为第二个%s(否则插入空字符串)。 kb.close()只是释放与您的扫描仪相关的资源。这将在您结束程序时自动发生,但最好手动进行。 – Dimpl

在第57行,你打开一个新的代码块,但没有任何东西能够访问它。我相信你想键入:由于布尔比较运算符返回true或false

if (month1 && day1 && year1) 
    System.out.println ("date is valid "); 

else{ 
     numDays = 28; 
     system.out.println("is not a leap year"); 
    } 

作为一个小技巧,你可以改变这一点:

if (month1 == true) 
       if (day1 == true) 
       if (year1 == true) 
         System.out.println ("date is valid "); 

本,你可以知道条件只是布尔值。由于month1day1year1都是布尔值,所以您不需要将它们与任何内容进行比较。

什么条件意味着,在你不知道的情况下,如果是和month1day1year1都是真实的,然后打印date is valid

你为什么不尝试的Java 8日期时间 API

它验证日期和做多的越多,你

try { 
     LocalDate date =LocalDate.of(2016, 12, 31); 
     if(date.isLeapYear()) 
      System.out.println("Leap year"); 
     else 
      System.out.println("Not leap year"); 
} 
catch(DateTimeException e) { 
    System.out.println(e.getMessage()); 
} 

程序的第50行if-else语句的语法不正确。 这是一个悬而未决的问题。在括号内包含if和else语句的主体应该可以解决这个问题。

if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) 
{ 
    numDays = 29;        
    system.out.println("is a leap year"); 
} 
else 
{ 
    numDays = 28; 
    system.out.println("is not a leap year"); 
} 

您可以利用IDE或注意编译器错误信息来解决此类错误。