所以我一直在研究这个程序,我得到了一些可以解决的日期,但是当我输入带有未来一年的二月时,它表明它应该是无效的。
出于这个计划的缘故,二月的日子只有1-28天。
谁能帮我发现我的错误?

  import java.util.Scanner;
    public class FutureDate {
     public static void main (String args[]){

  int inputMonth;
  int inputDate;
  int inputYear;
  final int currentMonth = 10;
  final int currentDate = 24;
  final int currentYear = 2013;
  final int numberOfDays = 31;

  Scanner keyboard = new Scanner(System.in);

  System.out.println("Enter a month in format - mm: ");
  inputMonth = keyboard.nextInt();

  System.out.println("Enter a day in format - dd: ");
  inputDate= keyboard.nextInt();

  System.out.println("Enter a year in format - yyyy: ");
  inputYear = keyboard.nextInt();


  if (inputYear < currentYear)
  System.out.println("Invalid Date");

  else if (inputYear >= currentYear)


  {

     if (inputMonth >= 1 && inputMonth <=12){

        if ((inputMonth == 4 || inputMonth == 6 || inputMonth == 9 || inputMonth == 11) && (inputDate >= 1 && inputDate <= 30))
        System.out.println("Valid Date");

        else if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=30)
        System.out.println("Valid Date");

        else if (inputMonth == currentMonth && inputDate > currentDate && inputDate <=30)
        System.out.println("Valid Date");

        else

        System.out.println("Invalid Date");
        }

     else if (inputMonth == 2 && inputDate >= 1 && inputDate <= 28){
        System.out.println("Valid Date");

        if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=28)
        System.out.println("Valid Date");

        else if (inputMonth == currentMonth && inputDate > currentDate && inputDate<=28)
        System.out.println("Valid Date");

        else
        System.out.println("Invalid Date");
        }

      else if (inputMonth == 1 || inputMonth == 3 || inputMonth == 5 || inputMonth == 7 || inputMonth ==10 || inputMonth == 12 && inputDate >= 1 && inputDate <= 31){

        if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=31)
        System.out.println("Valid Date");

        else if (inputMonth == currentMonth && inputDate > currentDate && inputDate <=31)
        System.out.println("Valid Date");

        else
        System.out.println("Invalid Date");

     }

      else
      System.out.println ("Invalid Date");

        }

  }

  }

最佳答案

您的第一个if子句检查它是否在1月和12月之间(2月是哪个),因此它将进入该if子句,而不是第二个子句。

如果先移第二个

10-07 11:58