简体   繁体   English

do-while循环似乎不起作用(Java)

[英]do-while loop doesn't seem to work (Java)

I think I found a solution to verify if a user-submitted string is in the correct format by using a do-while loop which executes as long as a boolean variable has the false value, however, after setting a breakpoint a saw that it exists the loop even though the variable is still set to false. 我想我找到了一种解决方案,可通过使用do-while循环来验证用户提交的字符串是否采用正确的格式,只要布尔变量具有假值,该循环便会执行,但是,在设置断点后,看到它存在即使变量仍设置为false,也会循环执行。 This is the code: 这是代码:

do {
    if (isLegit(x)) {
        try {
            dayTime = sdf.parse(x);
        } catch (Exception ex) {
            System.out.println("an exception was caught. oupsie. the day is now set to today.");
        }
        verify = true;
    } else {
        System.out.println("the format was wrong. please try again");
        x = in .nextLine();
        verify = false;
    }
} while (verify = false);

Boolean isLegit(String x) {
    try {
        if (Integer.parseInt(x.substring(0, 2)) > 0 && Integer.parseInt(x.substring(0, 2)) < 13) {
            if (Integer.parseInt(x.substring(3, 5)) > 0 && Integer.parseInt(x.substring(3, 5)) < 32) {
                if (Integer.parseInt(x.substring(6, 10)) > 1969 && Integer.parseInt(x.substring(6, 10)) < 2016) {
                    return true;
                }
            }
        }
    } catch (Exception ex) {
        return false;
    }
    return true;
}

What should I do about it? 我该怎么办?

The problem is here: 问题在这里:

while(verify=false);

Change it to: 更改为:

while(!verify);

while(verify=false)更改为while(verify==false)

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

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