簡體   English   中英

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

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

我想我找到了一種解決方案,可通過使用do-while循環來驗證用戶提交的字符串是否采用正確的格式,只要布爾變量具有假值,該循環便會執行,但是,在設置斷點后,看到它存在即使變量仍設置為false,也會循環執行。 這是代碼:

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;
}

我該怎么辦?

問題在這里:

while(verify=false);

更改為:

while(!verify);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM