我认为我找到了一个解决方案,通过使用do-while循环来验证用户提交的字符串是否格式正确,只要布尔变量具有false值就可以执行,但是,在设置断点之后执行锯即使变量仍然设置为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;
}
我该怎么办呢?
答案 0 :(得分:4)
问题在于:
while(verify=false);
将其更改为:
while(!verify);
答案 1 :(得分:1)
将while(verify=false)
更改为while(verify==false)