Dowhile循环似乎不起作用(Java)
我想我找到了一个解决方案,通过使用do while循环来验证用户提交的字符串的格式是否正确,只要布尔变量的值为false,该循环就会执行。但是,设置断点后,即使变量仍然设置为false,也会看到它存在于循环中。 代码如下:Dowhile循环似乎不起作用(Java),java,try-catch,do-while,Java,Try Catch,Do While,我想我找到了一个解决方案,通过使用do while循环来验证用户提交的字符串的格式是否正确,只要布尔变量的值为false,该循环就会执行。但是,设置断点后,即使变量仍然设置为false,也会看到它存在于循环中。 代码如下: do { if (isLegit(x)) { try { dayTime = sdf.parse(x); } catch (Exception ex) { System.out.print
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;
}
我该怎么办?将whileverify=false更改为whileverify==false将whileverify=false更改为whileverify==false问题在于:
while(verify=false);
将其更改为:
while(!verify);
问题在于:
while(verify=false);
将其更改为:
while(!verify);
非常感谢。我没有notice@IleanaProfeanu您是否可以接受答案?请在答案旁边选择一个“勾号”,这将阻止其他人回答并认为问题尚未解决:。谢谢!我没有notice@IleanaProfeanu您是否可以接受答案?请在答案旁边选择一个“勾号”,这将阻止其他人回答并认为问题尚未解决:。同时!验证更好!验证better@IleanaProfeanu不客气。记住“=”赋值“=”比较。继续走@请不要忘记将其中一个答案标记为已接受,这样像我这样的人就不会想到这个问题,认为它仍然需要回答。@IleanaProfeanu不客气。记住“=”赋值“=”比较。继续走@Ilenaprofeanu请不要忘了将其中一个答案标记为已接受,这样其他人就不会像我一样,认为这个问题仍然需要回答。