简体   繁体   English

Java while循环不重复

[英]Java while loop not repeating

I am writing this code where the user puts the username and password then I validate the entries in a database. 我将这段代码编写在用户放置用户名和密码的位置,然后验证数据库中的条目。 The problem with my code is that when the input is wrong it does not return and repeat the data input process from the while loop. 我的代码的问题在于,当输入错误时,它不会返回并重复while循环中的数据输入过程。 My code is as below. 我的代码如下。 If someone could help me fix it. 如果有人可以帮助我修复它。 Thank you in advance. 先感谢您。

boolean b = true;
while (b == true) {
    System.out.println("enter username");
    String username = scanner.next();
    System.out.println("enter password");
    String password= scanner.next();
    boolean result = userDao.validate(username, password);

    if (result == false) {
        System.out.println("password email do not match");
        scanner.next();
    } else {
        System.out.println("success");
        b = false;
    }
}

I don't have enough "reputation points" to provide a comment.. but curious if you would be able to remove scanner.next() within your if statement if that would do the trick. 我没有足够的“声誉点”提供评论..但很好奇您是否能够在if语句中删除scanner.next()以解决问题。

I tried it by setting the result to false and letting it run. 我通过将结果设置为false并使其运行来进行了尝试。

if (result == false) {
            System.out.println("password email do not match");
            //scanner.next();

        } else {
            System.out.println("success");
            b = false;
        }

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

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