为什么不满足条件后,Do While循环不会停止?

时间:2019-10-01 21:32:55

标签: java switch-statement boolean do-while

我编写了一个代码,该代码可以获取用户在开始日期和结束日期上的输入,并检查它们是否有效。在下面的代码中,我有2个do..while循环。一个用于开始日期,另一个用于结束日期。当执行第一个循环且不满足条件时,程序不会继续执行另一个do while循环。如果我可以收到解决此问题的方法,将会很有帮助。

int year, startMonth, endMonth, startDay, endDay; 
    boolean checkStartDate = false, checkEndDate = false;
    Scanner input = new Scanner (System.in);

    //checking Start Date
    do
    {   
    checkStartDate = false;
    System.out.print("Enter the year: ");
    year = input.nextInt();
    System.out.print("Enter the start month: ");
    startMonth = input.nextInt();
    System.out.print("Enter the start day: ");
    startDay = input.nextInt(); 

        switch (startMonth) 
        {
          case 1:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
          break;

          case 2:
       if(startDay <= 28)
        {
            checkStartDate = true;
        }
            break;

          case 3:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }

            break;

          case 4:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 5:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 6:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 7:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 8:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 9:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 10:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 11:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 12:
        if(startDay <= 31)
        {
            checkStartDate = true;
            return;     
        }
          default:
          checkStartDate = false;
          System.out.println("Try again and enter a valid date \n");
        }   
        checkStartDate = false;

    } while (checkStartDate = true);

    //checking End Date

    do
    {   
    checkEndDate = false;
    System.out.print("Enter the year: ");
    year = input.nextInt();
    System.out.print("Enter the start month: ");
    endMonth = input.nextInt();
    System.out.print("Enter the start day: ");
    endDay = input.nextInt(); 

        switch (endMonth) 
        {
          case 1:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }

        else
        {
            checkEndDate = false;
            System.out.println("Print a valid start day");
        }
          break;

          case 2:
       if(endDay <= 28)
        {
           checkEndDate = true;
        }
            break;

          case 3:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }

            break;

          case 4:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 5:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 6:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 7:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 8:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 9:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 10:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 11:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 12:
        if(endDay <= 31)
        {
            checkEndDate = true;
            return;
        }

          default:
            checkEndDate = false;
            System.out.println("Try again and enter a valid date \n");
        }   
        checkEndDate = false;

    } while (checkEndDate = true);

    System.out.println("correct ");

2 个答案:

答案 0 :(得分:2)

while (checkEndDate = true)

您正在分配 checkEndDate,因此循环将始终重复。您可能是说:

while (checkEndDate == true)

比较两个值。但是,由于您已经有一个布尔值,因此不需要进行比较:

while (checkEndDate)

请注意,通过组合类似的案例,可以大大减少代码量。例如:

case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
    if(startDay <= 31)
    {
        checkStartDate = true;
    }
    break;

30天的月份相同。

您还应该编写一个checkDate()方法,以免两次编写相同的代码。

答案 1 :(得分:0)

您在每个while循环的末尾添加了checkStartDate= false & checkEndDate = false;。这不是必需的。

使用布尔值将while条件更新为while (checkStartDate);

下面是工作代码

int year,startMonth,endMonth,startDay,endDay;         布尔值checkStartDate = false,checkEndDate = false;         扫描仪输入=新的扫描仪(System.in);

    //checking Start Date
    do
    {   
    checkStartDate = false;
    System.out.print("Enter the year: ");
    year = input.nextInt();
    System.out.print("Enter the start month: ");
    startMonth = input.nextInt();
    System.out.print("Enter the start day: ");
    startDay = input.nextInt(); 

        switch (startMonth) 
        {
          case 1:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
          break;

          case 2:
       if(startDay <= 28)
        {
            checkStartDate = true;
        }
            break;

          case 3:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }

            break;

          case 4:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 5:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 6:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 7:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 8:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 9:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 10:
        if(startDay <= 31)
        {
            checkStartDate = true;
        }
            break;

          case 11:
        if(startDay <= 30)
        {
            checkStartDate = true;
        }
            break;

          case 12:
        if(startDay <= 31)
        {
            checkStartDate = true;
            return;     
        }
          default:
          checkStartDate = false;
          System.out.println("Try again and enter a valid date \n");
        }   
       // checkStartDate = false;

    } while (checkStartDate);

    //checking End Date

    do
    {   
    checkEndDate = false;
    System.out.print("Check End Date... ");
    System.out.print("Enter the year: ");
    year = input.nextInt();
    System.out.print("Enter the start month: ");
    endMonth = input.nextInt();
    System.out.print("Enter the start day: ");
    endDay = input.nextInt(); 

        switch (endMonth) 
        {
          case 1:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }

        else
        {
            checkEndDate = false;
            System.out.println("Print a valid start day");
        }
          break;

          case 2:
       if(endDay <= 28)
        {
           checkEndDate = true;
        }
            break;

          case 3:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }

            break;

          case 4:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 5:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 6:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 7:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 8:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 9:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 10:
        if(endDay <= 31)
        {
            checkEndDate = true;
        }
            break;

          case 11:
        if(endDay <= 30)
        {
            checkEndDate = true;
        }
            break;

          case 12:
        if(endDay <= 31)
        {
            checkEndDate = true;
            return;
        }

          default:
            checkEndDate = false;
            System.out.println("Try again and enter a valid date \n");
        }   
      //  checkEndDate = false; 

    } while (checkEndDate);

    System.out.println("correct ");