2017-03-09 130 views
-1

我剛剛在我的學校開始使用C#(這不是家庭作業),並且是本站的新手。我試圖讓我自己的程序,將採取用戶輸入(或用戶名和密碼),然後比較用戶輸入與當前只有一行的數組。這是我的代碼的一部分...將用戶輸入與數組進行比較

int count = 0; 
     char delimiter = '#'; 
     string fileInput = ""; 
     string[] inputArray; 

     fileInput = inFile.ReadLine(); 
     while(fileInput != null) 
     { 
      inputArray = fileInput.Split(delimiter); 
      username[count] = inputArray[0]; 
      password[count] = inputArray[1]; 

/////////////

static void compare(string[] username, string[] password, int count, string userInputUsername, string userInputPassword) 
{ 
    userInputUsername = userInputUsername.ToLower(); 

    for (int x = 0; x < count; x++) 
    { 
     if (userInputUsername == username[x] && userInputPassword == password[x]) 
     { 
      loginSuccesful();     
     } 
     else if (userInputUsername != username[x] || userInputPassword != password[x]) 
     { 
      loginFailed(); // Seems to give the login failed message multiple times based on the # of lines in an array in the login.txt document. 
     } 
    } 
} 

我的陣列看起來像這樣,TJ#1(TJ作爲用戶名和1是密碼)。它正確地輸出密碼和用戶名,當我要求它,但每當我把我的userInputUsername和比較它的數組它總是給我我的loginFailed()方法。我有兩個userInputUsername和數組作爲字符串

+0

什麼是裏面*用戶名[x] * ?.如果它是用戶名然後將其轉換成小寫,如* username [x] .ToLower(); * – NNR

+0

用戶名[x]只是與數組, );對於我們在登錄時爲用戶名而被授予的輸入。 –

+0

然後,你必須拆分該數組,然後將其與* userInputUsername * – NNR

回答

-1

我認爲你應該使用「等於」比較,像這樣:

for (int x = 0; x < count; x++){ 
    if (userInputUsername.equals(username[x]) && userInputPassword.equals(password[x])) 
    { 
     loginSuccesful();     
    } 
    else if (!userInputUsername.equals(username[x]) || 
      !userInputPassword.equals(password[x])) 
    { 
     loginFailed(); // Seems to give the login failed message multiple times based on the # of lines in an array in the login.txt document. 
    } 
} 
+0

userInputUsername和userInputPassword被定義爲字符串,所以這將無法正常工作,除非我小提琴和解析它的權利? –

+0

是的,字符串的類型應該使用「等於」比較 – AMars

+0

@ T.Jackson請忽略此「建議」:AMars似乎誤讀標籤/代碼爲Java(不知道爲什麼,看起來很清楚C#給我) –

0

我一直無法與該擺弄,但我認爲這個問題是我是因爲試圖用一種方法返回兩件事而引起的。

相關問題