2016-11-30 164 views
-2

我在這個項目上工作了3個小時,但我不知道我在做什麼錯了。如果你能幫助我,我真的很感激它。問題是當我輸入密碼時。它說它是錯誤的密碼,即使我把正確的密碼它不允許我再次重試。如果用戶輸入錯誤的密碼,程序假設允許用戶嘗試3次在第三次之後程序必須關閉。如何讓用戶在c#中輸入3次密碼?

public partial class UserAndPin : Window 
{ 
    public UserAndPin() 
    { 
     InitializeComponent(); 
    } 

    private void btnOK_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      StreamReader sr = new StreamReader("Customer.txt"); 

      short attempts = 0; 
      string line; 

      while ((line = sr.ReadLine()) != null) 
      { 
       string[] lineArray = line.Split(';'); 
       if (lineArray[0] == txtName.Text & lineArray[1] == pbPassword.Password) 
       { 
        MainWindow mainWindow = new MainWindow(); 
        this.Hide(); 
        mainWindow.ShowDialog(); 
        //return; 
       } 
       else 
       { 
        attempts++; 
        if (attempts < 3) 
        { 
         MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more");              
        } 
        if (attempts == 3) 
        { 
         MessageBox.Show("Please try again later"); 
         this.Close(); 
        }       
       }      
      } 
      sr.Close(); 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
    } 
} 

}

+2

學會使用你的調試器! [如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –

+0

@Gurwinder你能解釋我多一點你的意思嗎? – Shahzada

+1

@Gurwinder使用'=='比較字符串有什麼問題? –

回答

0
Dictionary<string, string> loginInfo; 
short attempts = 0; 

public UserAndPin() 
{ 
    InitializeComponent(); 

    // Load the file to the dictionary 
    loginInfo = File.ReadAllLines("Customer.txt") 
     .Select(i => i.Split(';')) // Lines format: Username;Password 
     .ToDictionary(i => i[0].ToLower(), i => i[1]); // Username is the key of the dictionary 

} 

private void btnOK_Click(object sender, RoutedEventArgs e) 
{  
    var userId = txtName.Text.ToLower(); // Username ignore case 
    var password = pbPassword.Password; 

    if (loginInfo.ContainsKey(userId) && loginInfo[userId] == password) 
    { 
     // login success, show main window 
     MainWindow mainWindow = new MainWindow(); 
     this.Hide(); 
     mainWindow.ShowDialog(); 
     return; 
    } 

    // login fail, increment the count only 
    attempt++; 

    if (attempts < 3) 
    { 
     MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more");              
    } 
    if (attempts == 3) 
    { 
     MessageBox.Show("Please try again later"); 
     this.Close(); 
    } 

} 
+1

'while'沒有任何意義。 – Kinetic

+0

@Kinetic是的,你是對的 – Eric

1

既然你在聲明short attempts = 0;btnOK_Click每次內部單擊該按鈕將啓動attempts0,而你希望它增加1次每次用戶點擊該按鈕,這樣你需要在全球範圍內宣佈它像

public partial class UserAndPin : Window 
{ 
    short attempts; 
    public UserAndPin() 
    { 
     InitializeComponent(); 
     attempts = 0; 
    } 

attempts++;應該是while循環之下,因爲如果你在文件中10的用戶信息會增加或每當條件不匹配時增加attempt

現在如果用戶名和密碼與您正在閱讀的文件不匹配。如果用戶信息位於第10個位置或線路,它顯然不匹配,它會給你9個消息框。另一個問題是Logical AND operator&&,而不是&和比它會匹配所以正確的方法應該是

public partial class UserAndPin : Window 
{ 
    short attempts; 
    public UserAndPin() 
    { 
     InitializeComponent(); 
     attempts = 0; 
    } 

    private void btnOK_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      StreamReader sr = new StreamReader("Customer.txt"); 
      string line; 
      while ((line = sr.ReadLine()) != null) 
      { 
       string[] lineArray = line.Split(';'); 
       if (lineArray[0] == txtName.Text && lineArray[1] == pbPassword.Password) 
       { 
        MainWindow mainWindow = new MainWindow(); 
        this.Hide(); 
        mainWindow.ShowDialog(); 
        //return; 
       } 
      } 
      sr.Close(); 

      if (attempts < 3) 
      { 
       MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more"); 
      } 
      else 
      { 
       MessageBox.Show("Please try again later"); 
       this.Close(); 
      } 
      attempts++; //Since user has attempted it. 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
    } 
} 
+1

仍然存在'嘗試'應該在這段時間之外增加的問題。 – Kinetic

+0

是! @Kinetic。感謝 –

+0

的捕獲......並且'如果'他在檢查'attempt'值的地方不應該在'while'中。但是這非常接近。 – Kinetic

1

您應該用來初始化的「嘗試」每個成功的驗證。或在3小時內超過3次。

如果你在2次成功登錄,則「嘗試」應該是0

如果該項目不合格品你在3個多小時登錄時,「嘗試」也應被設置爲0。

+0

3小時從哪裏來? – Kinetic

1

首先你需要讀取你的文件來獲取所有的用戶名和密碼。理想情況下,您只需在構造函數中執行一次。

然後,你需要增加一個你的櫃檯。這個計數器需要在點擊事件處理程序之外聲明,就像其他答案中所述。

最後,您可以檢查輸入的用戶名和密碼是否與文件中的匹配。如果是這樣,你可以打開你的表格。如果沒有,則根據用戶是否已經到達第三次嘗試顯示其中一個消息框。

public partial class UserAndPin : Window 
{ 
    short attempts; 

    public UserAndPin() 
    { 
     InitializeComponent(); 
     attempts = 0; 
    } 

    private void btnOK_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      var users = File.ReadAllLines("Customer.txt") 
       .Select(line => new { login = line[0], password = line[1] }) 
       .ToList(); 

      attempts++; 

      if (users.Any(user => user.login == txtName.Text && user.password == pbPassword.Password)) 
      { 
       MainWindow mainWindow = new MainWindow(); 
       this.Hide(); 
       mainWindow.ShowDialog(); 
       return; 
      } 
      else 
      { 
       if (attempts < 3) 
       { 
        MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more"); 
       } 
       if (attempts >= 3) 
       { 
        MessageBox.Show("Please try again later"); 
        this.Close(); 
       } 
      } 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
    } 
} 
相關問題