2017-02-14 67 views
-4

我需要初始化一个密码。而密码不等于保密密码,并且尝试少于三次时会询问用户密码。如果密码正确,打印访问授权,如果密码不正确,打印访问被拒绝,最大尝试次数达到我需要创建一个密码检查器

import java.util.Scanner; 

public class PasswordChecker { 
    public static void main (String [] args) { 

    Scanner in = new Scanner(System.in); 

    String secretPassword = "alejandra"; 
    String password; 
    int attempts = 3; 

     System.out.println("Please enter password"); 
     password = in.nextLine(); 


     while (!password.equals(secretPassword) && attempts<3) 
    System.out.println ("incorrect try again"); 
     password = in.nextLine(); 
     if (password.equals (secretPassword) 
     System.out.println ("access granted"); 
    } 
} 
+0

我创建了一个无限循环。我是初学者。不知道如何解决它。 – ale

+0

看起来你需要学习使用调试器。请帮助一些[互补调试技术](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之后仍然有问题,请随时返回更多详情。 –

+0

[Password Checker]的可能重复(http://stackoverflow.com/questions/34360580/password-checker) – ale

回答

0

玩得开心。

如果你在强调counter,最好使用forloop而不是while循环,因为它会导致多次尝试。

public static void main (String [] args) { 

    Scanner in = new Scanner(System.in); 

    String secretPassword = "alejandra"; 
    String password; 
    int attempts = 3; 

     System.out.println("Please enter password"); 
     password = in.nextLine(); 


    for(int i=1;i<=attempts;i++){ 
    System.out.println ("Incorrect try again"); 
     password = in.nextLine(); 
     if (password.equalsIgnoreCase(secretPassword)){ 
     System.out.println ("access granted"); 
    } 
    } 
} 
+1

尽管您可能已经解决了此用户的问题,但对于出现此问题的用户,仅有代码的答案对这些用户来说并不是很有帮助在将来。请编辑您的答案,以解释为什么您的代码可以解决原始问题。 –

3

试试这个

  • 变化do while
  • 加括号
  • 组试图修正值
  • 添加)if
  • 增量attempts计数器

    Scanner in = new Scanner(System.in); 
    
    String secretPassword = "alejandra"; 
    String password; 
    int attempts = 1; 
    
    do { 
        System.out.println("Please enter password"); 
        password = in.nextLine(); 
        if (password.equals (secretPassword)) { 
         System.out.println ("access granted"); 
         break; 
        } 
        if (attempts < 3) 
         System.out.println ("incorrect try again"); 
        else 
         System.out.println ("failed");  
    } 
    while (attempts++ < 3); 
    

,或者如果你想有一个while循环液

Scanner in = new Scanner(System.in); 

    String secretPassword = "alejandra"; 
    String password; 
    int attempts = 0; 

    while (attempts++ < 3) { 
     System.out.println("Please enter password"); 
     password = in.nextLine(); 
     if (password.equals (secretPassword)) { 
      System.out.println ("access granted"); 
      break; 
     } 
     if (attempts < 3) 
      System.out.println ("incorrect try again"); 
     else 
      System.out.println ("failed"); 
    } 
+0

我只需要使用while循环。我怎样才能做到这一点? – ale

+0

没问题,看我的编辑 –

+0

我刚看到编辑!我认为我的电脑运行缓慢! – ale

0

你有一个无限循环,因为你不改变attempts变量。

取出password变量,并从while循环之外提示,改变你的while循环:

while(attempts > 0) { 
    System.out.print("Enter password: "); 
    if(in.nextLine().toLowerCase().equals(secretPassword)) { 
     System.out.println("access granted."); 
     break; 
    } 
    else { 
     System.out.println("Incorrect. Try again."); 
     attempts -= 1; 
    } 
} 
相关问题