嵌套“if”语句时,打破这个“for”循环

时间:2016-10-08 22:11:05

标签: java if-statement for-loop break

我正在尝试使用break;语句退出for循环。

final int NUM_USERS = 6;                     // Max Number of Users.
UserInfo[] users = new UserInfo[NUM_USERS];  // Array of Users.
int loginCounter = 0;                        // Counts bad login attempts.
int i = 0;                                   // Loop index variable.
String userRole = "";                        // Holds user's role text.

for (loginCounter = 1; loginCounter <= 3; ++loginCounter) {
        // Get User's Credentials.
        System.out.println("Enter Username: ");
        String username = input.next().toLowerCase();
        input.nextLine();                      // Allows User to enter password.
        System.out.println("Enter Password: ");
        String password = input.nextLine();

        String hash = sysLogin.convertToMd5(password);
        for (i = 0; i < users.length; ++i) {
            if (username.equals(users[i].getUsername())) {
                if (hash.equals(users[i].getHash())) {
                    userRole = users[i].getRole();
                    sysLogin.goodLogin();      // Prints Good Login message.
                    break;
                }
            } else {
                sysLogin.badLogin();           // Prints Bad Login message.
            }
        }
    }

发生了什么:

代码将在usernamepassword中读取,验证它是一个很好的登录,然后再次返回询问用户名和密码。

预期会发生什么:

一旦成功登录,它会将userRole设置为当前用户的角色,显示goodLogin消息,然后退出循环。

3 个答案:

答案 0 :(得分:2)

break只会突破内部for循环,而不是外部循环。

尝试做这样的事情

boolean login = false;
do {

    // Get User's Credentials.
    System.out.println("Enter Username: ");
    String username = input.next().toLowerCase();
    input.nextLine();                      // Allows User to enter password.
    System.out.println("Enter Password: ");
    String password = input.nextLine();

    // Convert password to MD5 hash.
    String hash = sysLogin.convertToMd5(password);
    for (i = 0; i < users.length; ++i) {
        if (username.equals(users[i].getUsername())) {
            if (hash.equals(users[i].getHash())) {

                login = true;

                userRole = users[i].getRole();
                sysLogin.goodLogin();      // Prints Good Login message.
                break;
            } else {

                sysLogin.badLogin();           // Prints Bad Login message.
            }
        }
    }
} while ( login == false && failCondition == false );

您需要将失败计数添加回循环,但这应该会给您一个更期望的结果。

答案 1 :(得分:1)

您可以附加标签并打破该标签(Java example about labels in branches):

final int NUM_USERS = 6;                     // Max Number of Users.
UserInfo[] users = new UserInfo[NUM_USERS];  // Array of Users.
int loginCounter = 0;                        // Counts bad login attempts.
int i = 0;                                   // Loop index variable.
String userRole = "";                        // Holds user's role text.
OUTER:
for (loginCounter = 1; loginCounter <= 3; ++loginCounter) {
        // Get User's Credentials.
        System.out.println("Enter Username: ");
        String username = input.next().toLowerCase();
        input.nextLine();                      // Allows User to enter password.
        System.out.println("Enter Password: ");
        String password = input.nextLine();

        // Convert password to MD5 hash.
        String hash = sysLogin.convertToMd5(password);
        for (i = 0; i < users.length; ++i) {
            if (username.equals(users[i].getUsername())) {
                if (hash.equals(users[i].getHash())) {
                    userRole = users[i].getRole();
                    sysLogin.goodLogin();      // Prints Good Login message.
                    break OUTER;
                }
            else {
                sysLogin.badLogin();           // Prints Bad Login message.
            }
            }
        }
    }

答案 2 :(得分:1)

break;语句打破了最内层的循环;最外面的循环将继续执行。

在最外层循环中,您需要一个变量,例如bool success = false;。在打破最里面的循环之前,分配true,并在最外面的循环中进行检查,如:

if(success) break;