限制登录尝试?

时间:2017-10-29 01:02:31

标签: java

我正在尝试将密码程序的登录尝试限制为3,但是当我使用while循环时,即使在第一次尝试时输入正确,也要求密码3次。这就是我到目前为止所做的:

import java.util.Scanner;

public class Password {

   public static void main(String[] args) {

   Scanner keyboard = new Scanner(System.in);

   String password, input;
   int maxAttempts;

   password = "Joe";
   maxAttempts = 0;

   while(maxAttempts < 3)
   {

   System.out.print("Enter your password: ");
   input = keyboard.nextLine();

   if(input.equals(password)) 

    System.out.println("Congratulations");

   else

    System.out.println("Incorrect. Try Again.");

    maxAttempts++;
   }

   }
}

2 个答案:

答案 0 :(得分:4)

您可以通过休息退出while循环;言。

在您的计划中更改以下内容

  if(input.equals(password)) 
      System.out.println("Congratulations");

  if(input.equals(password)) 
  {
      System.out.println("Congratulations");
      break;
  }

答案 1 :(得分:0)

上面说的没有中断。 while.only想要知道尝试是否超过3并不关心密码是否正确。