The output from this loop is in favor, while the nested if statements

I am trying to use an instruction break;to exit a loop 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 role text.

for (loginCounter = 1; loginCounter <= 3; ++loginCounter) {
        // Get User 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.
            }
        }
    }

What's happening:

The code will be read in usernameand passwordcheck that it is a good login, then go back to the username and password.

What is expected to happen:

After a successful login, it sets the userRolecurrent user role, displays a message goodLogin, and then exits the loop.

+4
source share
3 answers

( Java ):

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 role text.
OUTER:
for (loginCounter = 1; loginCounter <= 3; ++loginCounter) {
        // Get User 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.
            }
            }
        }
    }
+1

break , , .

-

boolean login = false;
do {

    // Get User 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 );

, .

+2

The operator break;breaks the loop of the innermost loop; the outer loop will continue to run.

In the outer loop, you must have a type variable bool success = false;. Before breaking the inner loop, assign trueand run a check in the outer loop, for example:

if(success) break;

+1
source

Source: https://habr.com/ru/post/1657160/


All Articles