如何在循环中添加有限的3次尝试?

c3frrgcw  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(286)

我正在尝试将最大登录尝试次数限制为3次。但是,我下面的代码使用了所有的尝试,我该如何解决这个问题?

import java.util.Scanner;
import java.util.*;

public class Final {

  public static void main(String[] args) {

    String Username;
    String Password;

    Password = "bruh"; //username and password for the code
    Username = "bro";

    int totalAttempts = 3; //attempt counter

    Scanner input1 = new Scanner(System.in);//scanner clasees
    System.out.println("Enter Username : ");
    String username = input1.next();

    Scanner input2 = new Scanner(System.in);
    System.out.println("Enter Password : ");
    String password = input2.next();

    //loop also using While statement that i learned from ap classroom

    if (username.equals(Username) && password.equals(Password)) {
        System.out.println("Welcome!Username and Password have been validated");
        return;
    } else {
        System.out.println("Username or password invalid");
        totalAttempts--;
    }

    if (totalAttempts == 0) {
        System.out.println("Maximum number of attempts exceeded");
    }
  }
}
tktrz96b

tktrz96b1#

Scanner input = new Scanner(System.in);
int attemptsLeft = 3;
while (attemptsLeft > 0) {
    System.out.println("Enter Username:");
    String username = input.nextLine();
    System.out.println("Enter password: ");
    String password = input.nextLine();
    if (username.equals(Username) && password.equals(Password)) {
        System.out.println("Logged in successfully");
        break;
    } else {
        System.out.println("Invalid username or password");
        attemptsLeft--;
    }
}
if (attemptsLeft == 0) {
    System.out.println("You ran out of tries");
}

如果凭证错误,这段代码几乎只使用while循环来重复这个过程。如果用户名和密码正确,那么行“break”将停止while循环,然后您可以继续执行正常功能。在while循环之外,它检查以确保attemptsleft不是0。如果是的话,上面写着“你尝试的次数不够了”。

cpjpxq1n

cpjpxq1n2#

有很多方法可以做到这一点。一种方法是把它放在 while 超过最大尝试次数或提供的凭据有效时中断的循环。 try-with-resources 以便在您使用完它之后关闭它。我想我没有看到扫描仪在原稿中被关闭。
你不需要两个扫描器,正如@timhunter在你可能想要使用的评论中提到的那样 nextLine() 在这里而不是仅仅 next() 可以做更多的改变,但这只是一些快速的事情。

更新的代码

public static void main(String[] args) {

    String Username = "bro";
    String Password = "bruh";

    int totalAttempts = 3;//attempt counter
    boolean validCredentials = false;

    try (Scanner scanner = new Scanner(System.in))  {
        while (totalAttempts > 0 && !validCredentials) {

            System.out.println("Enter Username : ");
            String username = scanner.nextLine();

            System.out.println("Enter Password : ");
            String password = scanner.nextLine();
            //loop also using While statement that i learned from ap classroom

            if (username.equals(Username) && password.equals(Password)) {
                validCredentials = true;
                System.out.println("Welcome!Username and Password have been validated");
            }

            else
            {
                totalAttempts--;
                System.out.println("Username or password invalid");
            }

        }
    }

    if (totalAttempts == 0) {
        System.out.println("Maximum number of attempts exceeded");
    }
}

相关问题