Java Networking - 限制登录尝试不循环

时间:2018-03-23 00:54:15

标签: java if-statement networking while-loop server

我正在制作一个简短的程序,用户只需输入服务器知道的用户名和密码。他们有4次尝试使它正确,如果他们没有输入正确的信息,他们将被踢出服务器。

如果用户第一次输入错误的登录信息,则允许他们再次输入用户名和密码,并告诉他们有3次尝试。但在此之后,它会卡住而不会再次循环。我该如何解决这个问题?

代码:

int tries = 4;
        while (tries>=0) {
            out.println(login);

            //login
            String username = in.readLine();
            System.out.println("Client's username: " + username);

            String password = in.readLine();
            System.out.println("Client's password: " + password);

            if (username.equals("hello123") && password.equals("mypass")) {
                out.println("Correct login!");
                System.out.println ("Client's IP Address: " + localaddr.getHostAddress());
            }

            else if (!username.equals("hello123")||!password.equals("mypass")) { //if wrong login - give 3 more tries

                tries--;
                System.out.println("Number of tries left: " + tries);
                out.println("Try again, enter username and password. Login attempts left - " + tries);

                //login
                username = in.readLine();
                System.out.println("Client's username: " + username);

                password = in.readLine();
                System.out.println("Client's password: " + password);
            }
        }
        if (tries==0){
            out.println("Wrong login - server closing");
            out.close();
            skt.close();
            srvr.close();
        }

3 个答案:

答案 0 :(得分:1)

听起来你需要让while循环继续运行,一旦你达到第0次尝试就把try重置为4。例如。

    while(true){
           //rest of the code
            if (tries==0){
                out.println("Wrong login - server closing");
                out.close();
                skt.close();
                srvr.close();
                tries = 4;
            }
}

您应该使用try / catch / finally或尝试使用资源以便正常退出。

答案 1 :(得分:1)

试试这个

int tries = 4;
        while (tries>=0) {
            out.println(login);

            //login
            String username = in.readLine();
            System.out.println("Client's username: " + username);

            String password = in.readLine();
            System.out.println("Client's password: " + password);

            if (username.equals("hello123") && password.equals("mypass")) {
                out.println("Correct login!");
                System.out.println ("Client's IP Address: " + localaddr.getHostAddress());
            }

            else if (!username.equals("hello123")||!password.equals("mypass")) { //if wrong login - give 3 more tries

                tries--;
                System.out.println("Number of tries left: " + tries);
                out.println("Try again, enter username and password. Login attempts left - " + tries);
            }
        }
        if (tries==0){
            out.println("Wrong login - server closing");
            out.close();
            skt.close();
            srvr.close();
        }

答案 2 :(得分:1)

删除if语句中的readLine(),并在用户名和密码正确时重置attempts = 4或者尝试== 0.最后将attempts == 0 if语句放入while循环。

  int tries = 4;
    while (tries>=0) {
        out.println(login);
        //login
        String username = in.readLine();
        System.out.println("Client's username: " + username);

        String password = in.readLine();
        System.out.println("Client's password: " + password);

        if (username.equals("hello123") && password.equals("mypass")) {
            out.println("Correct login!");
            System.out.println ("Client's IP Address: " + localaddr.getHostAddress());
            tries = 4;
        }

        else if (!username.equals("hello123")||!password.equals("mypass")) { //if wrong login - give 3 more tries

            tries--;
            System.out.println("Number of tries left: " + tries);
            out.println("Try again, enter username and password. Login attempts left - " + tries);
        }
       if (tries==0){
          out.println("Wrong login - server closing");
          tries = 4;
       }
    }