繁体   English   中英

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

[英]Why doesn't my Do While loop stop after the condition is not met?

我编写了一个代码,它获取用户在开始日期和结束日期的输入,并检查它们是否有效。 在下面的代码中,我有 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 ");
while (checkEndDate = true)

checkEndDate指定为 true,因此循环将始终重复。 你可能的意思是:

while (checkEndDate == true)

将比较两个值。 但既然你已经有了 boolean,你就不需要比较了:

while (checkEndDate)

请注意,您可以通过组合相似的案例来显着减少代码量。 例如:

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

30 天的月份也是如此。

您还应该编写一个checkDate()方法,这样您就不会重复编写相同的代码。

你添加了checkStartDate= false & checkEndDate = false; 每个 do while 循环结束。 这不是必需的。

将 while 条件更新为while (checkStartDate); 用 Boolean 值评估。

下面是工作代码

int 年,开始月,结束月,开始日,结束日; boolean 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 ");

暂无
暂无

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

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