2017-02-14 29 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; 
    } 
} 
相關問題